0

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
4
  • 1
    What are you learning from that it uses document.write? Lose that book/site and find something else. Commented Apr 30, 2012 at 1:27
  • 1
    Google search tip: if you add " mdn" after a Javascript keyword or function name (e.g., "split mdn") the first search result is almost always the Mozilla Developer Network doco page about that keyword or function Commented Apr 30, 2012 at 1:32
  • @epascarello—likely the OP is just using document.write as a convenient output method. Commented Apr 30, 2012 at 4:13
  • Correct, but I did originally copy the example from w3schools, entertainingly. Commented Apr 30, 2012 at 4:29

2 Answers 2

5

Just assign them to a variable when you split them:

var new_array = str.split(",");
answered Apr 30, 2012 at 1:02
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.