How do I separate a comma delimited variable into multiple variables?
This is probably very elementary, but I'm still learning.
I've imported a re开发者_如何学编程cord from an XML file and have the result : "a,b,c,d,e,f,g,h"I would like to end up with 8 separate variables, one for each comma delimited value.
What is the shortest way to code this using javascript?if you .split() the list, you'll end up with a single array with 'n' number of elements. Not -exactly- 8 separate variables, but 8 elements that can be accessed individually.
var myList = "a,b,c,d,e,f,g,h";
var myArray = myList.split( ',' );
alert( myArray[ 4 ] );
use split()
js>s = "a,b,c,d,e,f,g,h"
a,b,c,d,e,f,g,h
js>a = s.split(',')
a,b,c,d,e,f,g,h
js>a[0]
a
js>a[4]
e
Use split().
In your case, xml_string.split(',');
If you have the result as a string, use the split
method on it:
var myList = "a,b,c,d,e,f,g,h";
var myArray = myList.split(",");
精彩评论