0

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?

2
  • 1
    do you mean split? ~ Further learning: developer.mozilla.org/en/JavaScript/Reference/Global_Objects/… 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! Commented Dec 2, 2010 at 18:48

4 Answers 4

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 ] );
Sign up to request clarification or add additional context in comments.

5 Comments

+1 for answering the question that he asked "into separate variables" even tho I don't think he knows what he wants just yet, he just knows that it can be done somehow without parsing the array individually...
With this I can call each value...not as a variable(as I had originally understood) but instead as an element of an array. Kudos!
Glad that helped you. And since you seem to be relatively new, bear in mind that the first element of the array is the "0" element. So if the array has 8 elements, the first will be myArray[ 0 ], and the last will be myArray[ 7 ]. Sorry if that's pointing out something obvious, but wanted to make sure you knew.
LOL, I just found that out, good to know. Is that with all scripting or just C based languages?
Most languages use 0-based arrays. Some (like MATLAB) use 1-based arrays. Some (Pascal?) are flexible.
2

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

Comments

0

Use split().

In your case, xml_string.split(',');

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.