admin管理员组

文章数量:1026653

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 ma delimited value.
What is the shortest way to code this using javascript?

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 ma delimited value.
What is the shortest way to code this using javascript?

Share Improve this question edited Jun 25, 2013 at 19:30 Taryn 248k57 gold badges371 silver badges408 bronze badges asked Dec 2, 2010 at 17:14 KirtKirt 2,7232 gold badges18 silver badges11 bronze badges 2
  • 1 do you mean split? ~ Further learning: developer.mozilla/en/JavaScript/Reference/Global_Objects/… – jcolebrand Commented Dec 2, 2010 at 17:15
  • Yes, SPLIT, now a term I will never forget. I'm still learning the proper terminology. The theory is always so clear in my head...I just jumble syntax in the transcription, ha! – Kirt Commented Dec 2, 2010 at 18:48
Add a ment  | 

4 Answers 4

Reset to default 4

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(",");

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 ma delimited value.
What is the shortest way to code this using javascript?

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 ma delimited value.
What is the shortest way to code this using javascript?

Share Improve this question edited Jun 25, 2013 at 19:30 Taryn 248k57 gold badges371 silver badges408 bronze badges asked Dec 2, 2010 at 17:14 KirtKirt 2,7232 gold badges18 silver badges11 bronze badges 2
  • 1 do you mean split? ~ Further learning: developer.mozilla/en/JavaScript/Reference/Global_Objects/… – jcolebrand Commented Dec 2, 2010 at 17:15
  • Yes, SPLIT, now a term I will never forget. I'm still learning the proper terminology. The theory is always so clear in my head...I just jumble syntax in the transcription, ha! – Kirt Commented Dec 2, 2010 at 18:48
Add a ment  | 

4 Answers 4

Reset to default 4

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(",");

本文标签: javascriptHow do I separate a comma delimited variable into multiple variablesStack Overflow