Welcome to WebmasterWorld Guest from 107.20.54.98

Forum Moderators: open

Message Too Old, No Replies

Silly Array question

     
4:11 pm on Nov 2, 2007 (gmt 0)

Junior Member

5+ Year Member

joined:July 28, 2006
posts: 168
votes: 0


Hi I am not too good with Javascript but I had probably a simple question.

Say i have the following array:

arr[53] = "apples";
arr[27] = "oranges";
arr[38] = "lemons";

And I want to loop through each element of the array and get the key and the Value?

I would do:

for(i=0;i<arr.length;i++){
//How would i echo the key/value at this point?
}

Thanks!
Ryan

6:27 pm on Nov 2, 2007 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts:4966
votes: 10


Note, you're using an indexed array, not an associative array. So you don't access the values in the array with a string "key", you access them with an integer "index".

In your example, you're going to have an array that contains at least 54 items (your highest index is 53, and JavaScript arrays are zero indexed). If you do something like this:


for( var i = 0; i < arr.length; i++) {
alert("arr[" + i + "] = " + arr[i]);
}

Then arr.length is going to be 54, i represents the index, and arr[i] will get the value. In this case, if you've only populated 3 values in the array, you're going to be looping through a bunch of 'undefined' values.

6:38 pm on Nov 2, 2007 (gmt 0)

Junior Member

5+ Year Member

joined:July 28, 2006
posts: 168
votes: 0


If i only have 3 values (in this case i do) then yes there will be a whole bunch of undefined values and it would require an additional if statement in the loop to see if the variable is defined before preforming an action on the variable.

I did find another usefull method.


for(var i in arr) {
alert(i+" is "+arr[i]);
}

7:22 pm on Nov 2, 2007 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts:4966
votes: 10


Right, that will get only the items that you've defined. I almost posted that in my last post, but I wanted to stress the point that you're assigning values using a numeric index, so treating them as an associative array is probably not a good idea.
7:26 pm on Nov 2, 2007 (gmt 0)

Junior Member

5+ Year Member

joined:July 28, 2006
posts: 168
votes: 0


Yea i guess it looks that way. Those ID's are actually primary key's in a database, and the current array only contains specific rows. So yes although they are integers, they still need to be associative.

Thanks,
Ryan

9:29 pm on Nov 2, 2007 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts:4966
votes: 10


In that case, you're actually storing an "object" that has an id value and a string value. Perhaps something like this might better suit your needs:


var arr = [
{id : 53, fruit: "apples"},
{id : 27, fruit: "oranges"},
{id : 38, fruit: "lemons"}
];
for (var i = 0; i < arr.length; i++) {
alert(arr[i].fruit + " has an id of " + arr[i].id);
}

This treats the contents of the array as objects.