By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,663 Members | 1,455 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,663 IT Pros & Developers. It's quick & easy.

Problem with for/in loop over associative array

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
> 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..?


Your for should be

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

http://www.crockford.com/javascript/survey.html
Jul 23 '05 #2

P: n/a
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.
Jul 23 '05 #3

P: n/a
>> 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.


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
Jul 23 '05 #4

P: n/a
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.
Jul 23 '05 #5

P: n/a
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.
Jul 23 '05 #6

P: n/a
In article <d4**********@azure.qinip.net>, Yereth Jansen
<ya*********@spam.cs.uu.nl> writes
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.


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
Jul 23 '05 #7

P: n/a
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
Jul 23 '05 #8

P: n/a
rh
Csaba Gabor wrote:
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);


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

Jul 23 '05 #9

P: n/a
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
Jul 23 '05 #10

P: n/a
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.

Thanks for the help!

Yereth

Jul 23 '05 #11

P: n/a
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
Jul 23 '05 #12

P: n/a
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
Jul 23 '05 #13

P: n/a
rh
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

Jul 23 '05 #14

P: n/a
In article <42**********************@news.free.fr>, Alexis Nikichine
<al**************@somedomain.fr> writes
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 ?


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
Jul 23 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.