i have a JavaScript array of string arrays: array1; array2 ; array3 which i need to break and then access the individual array. how can i do that?
+3
A:
You can use the split
method:
var str = 'array1; array2 ; array3';
var arr = str.split('; ');
To access the individual array parts, you need to use index which starts from 0:
alert(arr[0]); // shows first item
alert(arr[1]); // shows second item
alert(arr[2]); // shows third item
Or you can loop through the array like this too:
for(var i = 0; i < arr.length; i++)
{
alert(arr[i]);
}
Sarfraz
2010-08-24 03:17:41
i think it's better if you split it with just ";", then just trim it.. so leading and trailing spaces will be omitted...
Manie
2010-08-24 03:22:15
how can i access the elements in the array
mazhar kaunain baig
2010-08-24 04:00:03
@mazhar kaunain baig: See my updated answer please.
Sarfraz
2010-08-24 08:29:39