If I have a string of names separated by commas, how do I separate them all and place them as individual elements in a new array?
For example--
var str="How,are,you,doing,today?";
document.write(str.split(","));
I'd like to put each of those elements straight into an array. What's the way to do this?
asked Apr 30, 2012 at 1:01
porque_no_les_deux
4791 gold badge6 silver badges18 bronze badges
2 Answers 2
Just assign them to a variable when you split them:
var new_array = str.split(",");
answered Apr 30, 2012 at 1:02
John Conde
220k100 gold badges464 silver badges504 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
If you're talking about how to you just declare it as an array, you would do that like this:
var strArray = ["How","are","you","doing","today?"];
answered Apr 30, 2012 at 1:15
jfriend00
711k104 gold badges1.1k silver badges1k bronze badges
Comments
lang-js
document.write? Lose that book/site and find something else.document.writeas a convenient output method.