문제

This is probably very elementary, but I'm still learning.
I've imported a record 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(",");
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top