Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > question about for...in and objects

Reply
Thread Tools

question about for...in and objects

 
 
ablock
Guest
Posts: n/a
 
      08-10-2007
I have an array to which i have a added a method called contains. I
would like to transverse this array using for...in...I understand
fully that for...in is really meant for Objects and not Arrays, but I
purposely had this array filled unsequentially because the key for the
array is meant to act as an ID which has a contextual meaning in my
script.
The problem, of course, is that for...in also returns my method
'contains' as one of the keys. Is there a way around this?

 
Reply With Quote
 
 
 
 
Stevo
Guest
Posts: n/a
 
      08-10-2007
ablock wrote:
> I have an array to which i have a added a method called contains. I
> would like to transverse this array using for...in...I understand
> fully that for...in is really meant for Objects and not Arrays, but I
> purposely had this array filled unsequentially because the key for the
> array is meant to act as an ID which has a contextual meaning in my
> script.
> The problem, of course, is that for...in also returns my method
> 'contains' as one of the keys. Is there a way around this?


You could check the typeof each item found in the loop. For example if
(typeof myarray[i]=="function") then that's your contains function.
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      08-10-2007
ablock wrote:
> I have an array to which i have a added a method called contains. I
> would like to transverse this array using for...in...I understand
> fully that for...in is really meant for Objects and not Arrays,


No, unfortunately you don't understand at all. Arrays are objects --
Array objects, having Array() as their constructor.

> but I purposely had this array filled unsequentially because the key
> for the array is meant to act as an ID which has a contextual meaning
> in my script.


That would be unlikely, since the "keys" of an array data structure
encapsuled by an Array object are integers (the indexes of the elements),
and ID values are not allowed to be integers in SGML-based markup languages
(like HTML).

Probably you have added some properties with non-numeric name to your Array
object, which you could have with any native ECMAScript object (CMIIW), as
they all inherit from Object.prototype. In that case, you should use an
Object object instead of an Array object, unless you try implementing PHP
associative arrays in an ECMAScript implementation.

> The problem, of course, is that for...in also returns my method
> 'contains' as one of the keys. Is there a way around this?


Not in current client-side ECMAScript implementations, and that is probably
what you are looking for. User-defined properties can't be given the
DontEnum attribute there.


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      08-10-2007
ablock said:
>
>I have an array to which i have a added a method called contains. I
>would like to transverse this array using for...in...I understand
>fully that for...in is really meant for Objects and not Arrays, but I
>purposely had this array filled unsequentially because the key for the
>array is meant to act as an ID which has a contextual meaning in my
>script.


So why use an Array at all? Use an Object.

>The problem, of course, is that for...in also returns my method
>'contains' as one of the keys. Is there a way around this?


Yes. Simply ignore it if the index=="contains".
If you're going to be adding more methods, check the value of
typeof index
or, if your valid indices are numbers, ignore indices for which
isNaN(index) is true.


--

 
Reply With Quote
 
ablock
Guest
Posts: n/a
 
      08-10-2007
> No, unfortunately you don't understand at all. Arrays are objects --
> Array objects, having Array() as their constructor.
>


Yes, i am aware of that...what i meant is that for...in was designed
for those objects that don't have an integer as their keys and
therefore are not 'associative'. I am fully aware that 'associative
arrays' in ECMAScript are actually objects. I do know how to program
object oriented ECMAScript.


> That would be unlikely, since the "keys" of an array data structure
> encapsuled by an Array object are integers (the indexes of the elements),
> and ID values are not allowed to be integers in SGML-based markup languages
> (like HTML).
>
> Probably you have added some properties with non-numeric name to your Array
> object, which you could have with any native ECMAScript object (CMIIW), as
> they all inherit from Object.prototype. In that case, you should use an
> Object object instead of an Array object, unless you try implementing PHP
> associative arrays in an ECMAScript implementation.
>


I have no idea what you're talking about...you are clearly taking a
big problem and making it a huge one.

I have an array (object) as follows: a = {'1' = 'something', '6' =
'something else'......

My keys represent id's that have a contextual meaning in my script. I
don't know why you think that that's unlikely, or why you think that I
meant id's in an SGML tag.

In my case they happen to represent id's from a database table.

Now it's true...I am using the Array object, when i could be using the
Object object or any other object for that matter. That's besides the
point. The point is that my object, let's call it hotdog for arguments
sake, has a method added to it, and i wish to transverse this through
for...in, so I repeat:

> > The problem, of course, is that for...in also returns my method
> > 'contains' as one of the keys. Is there a way around this?

>



> Not in current client-side ECMAScript implementations, and that is probably
> what you are looking for. User-defined properties can't be given the
> DontEnum attribute there.
>


This is the answer I should have received at the beginning of this
whole thing, so I wouldn't have wasted my time reading through the
whole message.

Thank you

 
Reply With Quote
 
ablock
Guest
Posts: n/a
 
      08-10-2007

> So why use an Array at all? Use an Object.


Because that doesn't solve my problem...

> Yes. Simply ignore it if the index=="contains".
> If you're going to be adding more methods, check the value of
> typeof index
> or, if your valid indices are numbers, ignore indices for which
> isNaN(index) is true.
>
> --

I am aware of all these solutions but they don't "solve" the
problem...thanks anyway

 
Reply With Quote
 
Justin McConnell
Guest
Posts: n/a
 
      08-10-2007
On Aug 10, 10:54 am, ablock <atbl...@gmail.com> wrote:
> The problem, of course, is that for...in also returns my method
> 'contains' as one of the keys. Is there a way around this?


See http://yuiblog.com/blog/2006/09/26/for-in-intrigue/ for a
workaround.


 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      08-10-2007
ablock said:

>This is the answer I should have received at the beginning of this
>whole thing, so I wouldn't have wasted my time reading through the
>whole message.


Do you suppose it's possible that the reason you got a lot of
answers that you didn't want was because you didn't ask the
right question to begin with?

Thanks for playing.


--

 
Reply With Quote
 
ablock
Guest
Posts: n/a
 
      08-10-2007
> Do you suppose it's possible that the reason you got a lot of
> answers that you didn't want was because you didn't ask the
> right question to begin with?


No i didn't realize it...considering my question was clear enough that
even you answered it...albeit not with an answer that helped my cause.

> Thanks for playing.


You lose.



 
Reply With Quote
 
ablock
Guest
Posts: n/a
 
      08-10-2007
On Aug 10, 4:52 pm, Justin McConnell <bool...@gmail.com> wrote:
> On Aug 10, 10:54 am, ablock <atbl...@gmail.com> wrote:
>
> > The problem, of course, is that for...in also returns my method
> > 'contains' as one of the keys. Is there a way around this?

>
> Seehttp://yuiblog.com/blog/2006/09/26/for-in-intrigue/for a
> workaround.


Thank you.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to store and represent 1 objects relationship to other objects pat.trainor@gmail.com Java 7 10-29-2012 02:12 PM
Passing data between objects and calling all objects of a class in turn ghoetker Python 1 08-25-2010 03:18 AM
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
Question about objects in objects. JoeC C++ 6 10-05-2006 07:52 PM
objects of objects, vectors and sessions bigbinc Java 3 11-18-2003 09:26 AM



Advertisments