Pergunta

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?

Foi útil?

Solução

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 ] );

Outras dicas

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(",");
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top