question about for...in and objects

A

ablock

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?
 
S

Stevo

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.
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=="function") then that's your contains function.
 
T

Thomas 'PointedEars' Lahn

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,

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
 
L

Lee

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.


--
 
A

ablock

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:
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
 
A

ablock

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
 
L

Lee

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.


--
 
A

ablock

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.
 
L

Lee

ablock said:
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.

And you still don't realize that we were answering the question that
you asked. Just not the one that you *meant* to ask.

Your gratitude is going to ensure that many people will rush to help
you in the future.


--
 
T

Thomas 'PointedEars' Lahn

ablock said:
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.

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

Obviously you have no clue what I am talking about because you are lacking
basic knowledge.
I have an array (object) as follows: a = {'1' = 'something', '6' =
'something else'......

`a' is *not* assigned a reference to an Array object here.

It could be a reference to an Object object, initialized with an Object
literal, if the "inner" `=' would be replaced by `:'.

It could be a reference to an Array object, initialized with an Array
literal, if `{' and `}' were replaced by `[' and `]', respectively, and the
"inner" `=' were replaced by `,'.

Other than that, it is merely a big syntax error in ECMAScript
implementations, because strings are immutable and you are trying to assign
(`=') literally 'something' to a string (literal).

However, curly braces are part of the syntax for a Java array. So it may as
well be that you have asked in the wrong newsgroup. In that case, try one
of comp.lang.java.* instead. Or you have simply not understood that
JavaScript has nothing to do with Java but the first four characters.
Probably we'll never know.

Had you posted *some* code in the first place, this could have been
clarified right away.
Now it's true...I am using the Array object,

No, you are not. So far, you are not using anything.
when i could be using the Object object or any other object for that matter.
That's besides the point.

This is not a support forum. I post on Usenet whatever comment I deem
necessary regarding your article. And I found it necessary to point out
that you waste memory and (probably) run time, one cause of your not
understanding the very *basics* of the language but immodestly claiming
otherwise.
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:

How boring.
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.

Probably not.


PointedEars
 
A

ablock

ablock said:





And you still don't realize that we were answering the question that
you asked. Just not the one that you *meant* to ask.

Your gratitude is going to ensure that many people will rush to help
you in the future.

--

Not sure what you're talking about...I never said anybody didn't
answer my question. I said that the answers given didn't *address* my
problem. In otherwords, yes, those solutions work, but they are
bandaids...they don't solve the *root* of the issue. Everybody here
answered my question, just not with an answer that I already didn't
know. So I don't know what you meant by "the question that *meant*
to ask".

I'm also not sure what you were implying in your last statement since
I was careful to thank everybody who gave an answer...including you.

Thank you for your time.
 
A

ablock

No, unfortunately you don't understand at all. Arrays are objects --
Apparently you don't.

Why is that so apparent?
Obviously you have no clue what I am talking about because you are lacking
basic knowledge.

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

`a' is *not* assigned a reference to an Array object here.

It could be a reference to an Object object, initialized with an Object
literal, if the "inner" `=' would be replaced by `:'.

It could be a reference to an Array object, initialized with an Array
literal, if `{' and `}' were replaced by `[' and `]', respectively, and the
"inner" `=' were replaced by `,'.

Other than that, it is merely a big syntax error in ECMAScript
implementations, because strings are immutable and you are trying to assign
(`=') literally 'something' to a string (literal).

This was not my syntax...I just demonstrating a point. Please stick to
the topic at hand...

Since you keep missing the point, maybe i'll repeat it again in
clearer language.

I don't care if it's you wanna call it an array, an object or a
hotdog. At this point its a semantic argument and has no relevance to
the discussion at hand. I just have a hotdog which is not filled
sequentially, so that indice '1' has a value, and '6' has a value,
etc. I just want to iterate through my hotdog but apparently I learned
that I can't do that so simply in ECMAScript.

I'm not sure if I can make this much clearer than this. Of course, I'm
not asking a question anymore so this all a mute point.
However, curly braces are part of the syntax for a Java array. So it may as
well be that you have asked in the wrong newsgroup. In that case, try one
of comp.lang.java.* instead. Or you have simply not understood that
JavaScript has nothing to do with Java but the first four characters.
Probably we'll never know.

This is totally uncalled for. Learn some manners.
Had you posted *some* code in the first place, this could have been
clarified right away.

I asked a totally general question. There is no point in posting code.
Apparently all the other people who answered my question were able to
do so without me posting code.
No, you are not. So far, you are not using anything.

Sorry...I meant my hotdog...my bad...
This is not a support forum.
Therefore?


necessary regarding your article. And I found it necessary to point out
that you waste memory and (probably) run time, one cause of your not

What I waste in memory i gain back in speed by saving my self an extra
loop...
Probably not.

Actually...yes...this is the answer that I should have gotten in the
beginning...

When you answer questions, try to actually answer them instead of
trying to show off how nerdy you are like all the other fellows on
this forum.
 
L

Lee

ablock said:
ablock said:





And you still don't realize that we were answering the question that
you asked. Just not the one that you *meant* to ask.

Your gratitude is going to ensure that many people will rush to help
you in the future.

--

Not sure what you're talking about...I never said anybody didn't
answer my question. I said that the answers given didn't *address* my
problem. In otherwords, yes, those solutions work, but they are
bandaids...they don't solve the *root* of the issue. Everybody here
answered my question, just not with an answer that I already didn't
know. So I don't know what you meant by "the question that *meant*
to ask".

I'm also not sure what you were implying in your last statement since
I was careful to thank everybody who gave an answer...including you.


The point you're missing is that you asked for solutions, but
actually rejected any that didn't solve the *root* of the issue.
That's not the normal criteria for acceptable answers, so you
should have made it clear from the beginning.

"I'm aware of these solutions but they don't "solve" the problem,
thanks anyway", sounds more like sarcasm than gratitude.
At this point, I'll assume it's just more miscommunication.


--
 
G

Gregor Kofler

ablock meinte:
When you answer questions, try to actually answer them instead of
trying to show off how nerdy you are like all the other fellows on
this forum.

Well, you don't have to be nerdy, to know the difference between a
newsgroup and a "forum". Anyway, since you won't post here anymore,
you'll probably never know - what a loss...

Gregor
 
A

ablock

ablock meinte:


Well, you don't have to be nerdy, to know the difference between a
newsgroup and a "forum". Anyway, since you won't post here anymore,
you'll probably never know - what a loss...

Gregor

I don't mean to start an argument about this but please enlighten me
as to the difference between a newsgroup and a forum...
http://en.wikipedia.org/wiki/Newsgroup
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top