1

What is the best way to convert the string values to an int array, e.g.:

var s = '1,1,2';

to:

var a = [1,1,2];

Thanks

3
  • Do you trust the values in the string array or do you need to validate that each value is numeric? Commented Feb 10, 2011 at 16:58
  • I was thinking about using split then parseInt to make sure, the values should be ok though Commented Feb 10, 2011 at 17:01
  • If you trust the values, the solution in the first part of @Andy E's answer is the simplest and quickest. Commented Feb 10, 2011 at 17:04

3 Answers 3

5
"1,2,3".split(",").map(Number);

And for those browsers that don't implement map, take an implementation like this one from here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map

Array.prototype.map is in ECMAScript5, so don't be afraid to augment Array.prototype.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for the map idea - although the compatibility implementation would be a little slow compared to a standard loop.
Difference between loop and wrapped loop can be neglected.
3

JavaScript is a dynamic-typed language, which means that it might not be so important for those array items to be numbers. If that's the case, you might want to consider just using split():

var s = '1,1,2',
    a = s.split(",");

If it is important that they're numbers, then your best bet is to iterate over them afterwards:

for (var i = 0, max = a.length; i < max; i++)
    a[i] = +a[i]; 

There's also the ECMAScript 5th Edition method map, but it's not implemented in all browsers yet.

3 Comments

Do you need to validate the strings are numeric first or do you already know that they'll be valid?
Thanks, is there an advantage of using a[i] = +a[i]; over a[i] = parseInt(a[i])?
@xylar: without a second argument for parseInt, there's a potential danger of numbers starting with a 0 being parsed as octals. +a[i] is the equivalent of Number(a[i]), which always parses as decimal unless the number starts with 0x (for hex). Mostly, though, I use it because it's shorter :-)
1

If the data is secure, you could use .eval().

var a = eval( '[' + s + ']');

If you're not sure about security, you could use JSON.parse.

var a = JSON.parse( "[" + s + "]" );

...though you'll need to include a parser in browsers that don't support it natively.

2 Comments

Forewarning that if any of the values were invalid, a syntax error would be thrown - e.g. 1,2,3a,4. If you can trust the source, then I quite like the JSON.parse approach.
@Andy E: Good point. This works if you're expecting valid numbers. Though if something invalid finds its way in, a syntax error may be desired. Depends on how this is used I guess. :o)

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.