homepage Welcome to WebmasterWorld Guest from 107.20.25.215
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Silly Array question
ryan_b83




msg:3494758
 4:11 pm on Nov 2, 2007 (gmt 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

 

Fotiman




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

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.

ryan_b83




msg:3494906
 6:38 pm on Nov 2, 2007 (gmt 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]);
}


Fotiman




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

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.

ryan_b83




msg:3494958
 7:26 pm on Nov 2, 2007 (gmt 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

Fotiman




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

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved