Forum Moderators: open

Message Too Old, No Replies

simple for loop prob

         

ahmedtheking

2:46 pm on Sep 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Rite, I've got an array with set keys. What I'd like to do is conver the array from 'Array ( KEY => VALUE )' to a string 'KEY>VALUE'. Any ideas? I've tried using the for loop, but just confusing myself!

penders

4:17 pm on Sep 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think you need to use the 'for' loop if you have an associative (key => value) array...

var key, str = ''; 
for (key in myarray) {
if (str.length > 0) {str += ', '; }
str += key + '>' + myarray[key];
}
alert(str);

key - is just a variable to hold your array key (or object property)
myarray - is your array variable
str - is the resultant string with all the array (key>value) pairs concatenated, separated by ', ' (comma space).