Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem with for/in loop over associative array

1 view
Skip to first unread message

Yereth Jansen

unread,
Apr 18, 2005, 7:50:35 AM4/18/05
to
Hi all,

I encountered a problem with looping through an associative array. All
worked perfectly with the following code:

for (var menuItem in this.menuItems) {
doSomething();
}

where this.menuItems is an associative array. The problem occurred when
I added the following function to the Array prototype:

Array.prototype.print_r = function() {
print_the_array_code();
}

After adding this method, the for/in loop took print_r as an element as
well. So besides the items I actually wanted to get from my array, I
also go print_r, which obviously I don't want. Does anyone have a
suggestion what to do with this..?

Thanks in advance,
Yereth Jansen

Douglas Crockford

unread,
Apr 18, 2005, 8:02:48 AM4/18/05
to

Your for should be

for (var i; i < this.menuItems.length; i += 1) {

http://www.crockford.com/javascript/survey.html

Yereth Jansen

unread,
Apr 18, 2005, 8:12:38 AM4/18/05
to
Douglas Crockford wrote:
> Your for should be
>
> for (var i; i < this.menuItems.length; i += 1) {
>
> http://www.crockford.com/javascript/survey.html

That won't work. I am using an associative array as in a hash. An
associative array does not have a numerical index nor does it have the
property .length set like I want it. ".length" is 0 in all cases.

Douglas Crockford

unread,
Apr 18, 2005, 8:44:57 AM4/18/05
to

Do not use Array when you do not need .length. You should be using an
Object. It is best to not use the term "associative array" when working
in JavaScript because it will confuse you.

Use the typeof operator on the values you extract, and skip the ones
that are 'function'.

http://www.crockford.com/javascript/survey.html

Yereth Jansen

unread,
Apr 18, 2005, 9:24:52 AM4/18/05
to
Douglas Crockford wrote:
> Do not use Array when you do not need .length. You should be using an
> Object. It is best to not use the term "associative array" when working
> in JavaScript because it will confuse you.
>
> Use the typeof operator on the values you extract, and skip the ones
> that are 'function'.
>
> http://www.crockford.com/javascript/survey.html

Thank you. I was hoping for another answer than this one, but I
suspected it was the only 'solution'. No such luck I guess. Thanks for
helping.

Cheers.

Michael Winter

unread,
Apr 18, 2005, 1:45:59 PM4/18/05
to
On 18/04/2005 14:24, Yereth Jansen wrote:

[To avoid enumerating user-defined functions]

> Douglas Crockford wrote:

[snip]

>> Use the typeof operator on the values you extract, and skip the ones
>> that are 'function'.

[snip]

> Thank you. I was hoping for another answer than this one, but I
> suspected it was the only 'solution'. No such luck I guess.

Well, you could use your own hashtable implementation. You'd sacrifice
the ability to use square bracket notation, but you would have
consistent, controlled behaviour. The archives of this newsgroup (via
Google Groups) will have several implementations (including mine :).

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.

John G Harris

unread,
Apr 18, 2005, 3:50:34 PM4/18/05
to
In article <d40c60$sfj$1...@azure.qinip.net>, Yereth Jansen
<yajan...@spam.cs.uu.nl> writes

It's worth saying again : if you say "associative array" you are likely
to forget that they are objects with properties, and that a method is
just a property whose value happens to be a function object.

Also, it might not occur to you to build a prototype object to keep all
the methods out of the way.

John
--
John Harris

Csaba Gabor

unread,
Apr 18, 2005, 5:48:47 PM4/18/05
to
Yereth Jansen wrote:
> All worked perfectly with the following code:
>
> for (var menuItem in this.menuItems) {
> doSomething();
> }
>
> where this.menuItems is an associative array. The problem occurred when
> I added the following function to the Array prototype:
>
> Array.prototype.print_r = function() {
> print_the_array_code();
> }

It's not exactly clear what you want (ie. on what basis you wish the
print_r to be filtered), but perhaps this quickly-thrown-together
function comes close to what you are looking for:

function showNonPrototypes(obj) {
var oForbidden = {}; // list of keys on the prototype chain
var prType = ""; // the prototype type
var idx;
if (typeof(obj)=="object") {
var objType = ""+obj; // looking for [object prType]
if ((obj.length + "")!="undefined") prType = "Array";
else if (objType.substring(0,7)=="object ")
prType = objType.substring(7);
if (prType && window[prType] && window[prType].prototype)
for (idx in window[prType].prototype) oForbidden[idx] = 1;
for (idx in obj)
if (!oForbidden[idx])
alert(idx); // doSomething();
}
}

// Example
Object.prototype.print_r = function () { return "nice array code"; };
var aTest = []; // starts life as an array
aTest.x = "y";
aTest[2] = 45;
aTest.z = "borf";
showNonPrototypes(aTest);


Csaba Gabor from Vienna

rh

unread,
Apr 19, 2005, 11:20:01 PM4/19/05
to

That seems rather obtuse -- it could be much better accomplished by
making use of the "hasOwnProperty" function:

for (var k in aTest) {
if(aTest.hasOwnProperty(k)) {
alert("Has own property "+k + " : "+aTest[k]);
}
}

If there's a concern for older browsers that don't provide this
function, a reasonably good equivalent can be added as:

if (! Object.prototype.hasOwnProperty) {
Object.prototype.hasOwnProperty = function(pty) {
return (typeof this[pty] != "undefined" &&
(this.constructor
&& this.constructor.prototype
&& this.constructor.prototype[pty] !== this[pty]))
}
}

../rh

Yereth Jansen

unread,
Apr 20, 2005, 4:17:02 AM4/20/05
to
Michael Winter wrote:
> Well, you could use your own hashtable implementation. You'd sacrifice
> the ability to use square bracket notation, but you would have
> consistent, controlled behaviour. The archives of this newsgroup (via
> Google Groups) will have several implementations (including mine :).
>
> Mike


It crossed my mind and I've seen the sample implementations. Not sure if
it was yours, but if I'll use it I'll make sure to credit your work. ;)
Right now I've decided to not define prototype functions for array, but
rather functions that take an array as an argument, like php does it. I
was just a bit too much into OO programming (yes, even with javascript),
so I was quite stubborn in wanting it to be 'native' functions of the
object.

Anyhue, I will go for the external functions for now, until it doesn't
suite me any longer.

Thanks for the help!

Yereth

Yereth Jansen

unread,
Apr 20, 2005, 4:22:08 AM4/20/05
to
John G Harris wrote:
> It's worth saying again : if you say "associative array" you are likely
> to forget that they are objects with properties, and that a method is
> just a property whose value happens to be a function object.

I actually did not realize this before, but before I posted this I've
realized that associative arrays were more of a hack than a native
datastructure in javascript. The tutorials never told me, unfortunately.
Or I just plainly missed it as I was not aware of the possibility.

> Also, it might not occur to you to build a prototype object to keep all
> the methods out of the way.

See my other reply on Michael's mail. Customizing is out of the question
for now. Mainly because the necessity is not really there.

Yereth Jansen

unread,
Apr 20, 2005, 4:26:57 AM4/20/05
to
rh wrote:
> That seems rather obtuse -- it could be much better accomplished by
> making use of the "hasOwnProperty" function:
>
> for (var k in aTest) {
> if(aTest.hasOwnProperty(k)) {
> alert("Has own property "+k + " : "+aTest[k]);
> }
> }
>
> If there's a concern for older browsers that don't provide this
> function, a reasonably good equivalent can be added as:
>
> if (! Object.prototype.hasOwnProperty) {
> Object.prototype.hasOwnProperty = function(pty) {
> return (typeof this[pty] != "undefined" &&
> (this.constructor
> && this.constructor.prototype
> && this.constructor.prototype[pty] !== this[pty]))
> }
> }
>
> ../rh

There is no concern for older browsers or any other browser that I am
testing with as I am producing a content management system. We set the
demands. :-)

Thanks for the help both Csaba and rh. This will help me to write my
implementation most fit for the job.

Cheers,
Yereth

Alexis Nikichine

unread,
Apr 20, 2005, 6:00:50 AM4/20/05
to
John G Harris wrote:
> It's worth saying again : if you say "associative array" you are likely
> to forget that they are objects with properties, and that a method is
> just a property whose value happens to be a function object.
>
> Also, it might not occur to you to build a prototype object to keep all
> the methods out of the way.

I am afraid I don't understand how building a prototype object will keep
all the methods out of the way. As far as I know, it will keep them out
of the way of the delete operator, but not out of the way of for-in
enumerations, which is the relevant point for the current problem.

Could you please elaborate some further on this ?

Alexis

--
Some domain is free

rh

unread,
Apr 20, 2005, 1:19:41 PM4/20/05
to
Yereth Jansen wrote:
> rh wrote:

<..>

> > for (var k in aTest) {
> > if(aTest.hasOwnProperty(k)) {
> > alert("Has own property "+k + " : "+aTest[k]);
> > }
> > }

<..>

> There is no concern for older browsers or any other browser that I am

> testing with as I am producing a content management system. We set
the
> demands. :-)
>

That's fine. One small caution here: There appears to be a bug in Opera
(7.5) with regard to hasOwnProperty when it involves a numeric
(-->string) property name . E.g.,

var x = { "2":true };
alert( x.hasOwnProperty("2") ); // *** false *** under Opera

This turned up when running Csaba's sample test.

../rh

John G Harris

unread,
Apr 20, 2005, 3:27:31 PM4/20/05
to
In article <42662857$0$7210$626a...@news.free.fr>, Alexis Nikichine
<alexis.n...@somedomain.fr> writes

Only to say you're right : for/in does indeed crawl up the prototype
chain (ECMA 262 v3, sec 12.6.4, last paragraph).

I blame the Mongolian vodka I'd sampled earlier in the day :-)

John
--
John Harris

0 new messages