473,326 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Problem with for/in loop over associative array

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
14 6672
> 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
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
>> 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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
2
by: Zenobia | last post by:
I have a problem. I need to look up several values stored in arrays. Each value is stored as a pair. The first (number) represents the probability of this item occurring. For instance, in the...
6
by: mark4asp | last post by:
Suppose I have the following code. It functions to randomly select a city based upon the probabilities given by the key differences in the associative array. . Eg. because the key difference...
3
by: pbali | last post by:
Hi, I am using PHP 5.1 and MySQL. I have a result set obtained by executing PDO:: query. I want to create an XML file by using this result set. The XML file will contain column names as XML node...
15
by: James Black | last post by:
If you go to http://dante.acomp.usf.edu/HomeworkAssistant/index.php you will see my code. Type in: s = a + b and hit tab, and you will see the extra words. How do I remove these? Here is a...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
11
by: Bosconian | last post by:
I'm trying to output the contents of an array of associative arrays in JavaScript. I'm looking for an equivalent of foreach in PHP. Example: var games = new Array(); var teams = new...
9
by: Jerim79 | last post by:
Here it is: <?php if($_SERVER=='POST'){ $Number=$_POST; $Email=$_POST; $Number2=0; $error=0;
11
by: SM | last post by:
I have a couple of arrays that represent different categories. One array per category. The arrays contain names of photos. The arrays look like this: $cat_1 = array('moonlight.jpg'',...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.