2

I have a string MyString like this:

var MyString = "32,43,545,34,23,";

I'd like to have this in an array of ints.

If I do this:

var MyArray = MyString.split(",");

I get an array of strings. How do I get an array of ints?

I know I can loop over the array and do a parseInt on each element but I was wondering if there's a better way to do it.

Thanks.

3
  • If you're setting the contents why can't you make them an array initially? Commented Apr 12, 2012 at 13:12
  • Don't capitalize random variable names, use "camelCase". Commented Apr 12, 2012 at 13:13
  • @Rick: the string is the result of an ajax callback. Commented Apr 12, 2012 at 13:27

3 Answers 3

7

unfortunatelly you'll have to manually walk through the array and change each element like this:

var MyArray = MyString.split(",");
for (var i=0, LoopTimes = MyArray.length; i < LoopTimes; i++){
    MyArray[i] = parseInt(MyArray[i], 10);
}

or you can also have a look at this: http://phpjs.org/functions/array_walk:349

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

3 Comments

Don't forget the radix to parseInt() It's optional, but really it isn't. parseInt(MyArray[i], 10)
Ok, that's what I thought of doing too. Oh well, no big biggie anyway.
I edited your answer to cache the length of the array and speed up the loop a bit. Thanks for your answer; I prefer cross-browser compatibility to something terser that works only in most browsers.
7
'32,43,545,34,23'.split(',').map(Number)

5 Comments

I like it too, but it doesn't look like it's supported until in IE until 9. developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…
For browsers not supporting ES5, you can use the excellent es5-shim.
"As closely as possible to ES5" is not very close. Many of these shims are intended only to allow code to be written to ES5 without causing run-time errors in older engines. In many cases, this means that these shims cause many ES5 methods to silently fail. Decide carefully whether this is what you want... Ouch... api.jquery.com/jQuery.map maybe?
@Rick: Wait... If that statement from es5-shim is enough to dissuade someone, then how would jQuery be an improvement? $.map doesn't even try to be ES5 compatible.
Well, I pasted that comment from the readme and it seems I may have skimmed it too fast. For some reason I mistook fail silently for 'failing silently and not doing anything at all' instead of 'fail silently but go ahead and do what you expect to happen with this other function' I suppose it could actually be a decent library.
0

This should work in most cases as well, and it is slightly faster than the parseInt method I believe.

for(var i=0;i<MyArray.length,i++){
  MyArray[i]=MyArray[i]-0;
}

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.