2
ids_nn
["50348", "18646", "17963", "18184", "30703", "18016", "23225"]

How do I make it:

[50348, 18646, 17963, 18184, 30703, 18016, 23225]

I read these two SO posts:

How to convert all elements in an array to integer in JavaScript?

convert string into array of integers

So, I tried:

var bla = ids_nn.map(function (x) { return parseInt(x, 10})
VM4765:2 Uncaught SyntaxError: Unexpected token }message: "Unexpected token }"stack: (...)get stack: function () { [native code] }set stack: function () { [native code] }__proto__: ErrorVM3550:847 InjectedScript._evaluateOnVM3550:780 InjectedScript._evaluateAndWrapVM3550:646 InjectedScript.evaluate

and

var bla = ids_nn.split(',').map(Number)
VM4648:2 Uncaught TypeError: undefined is not a function
3
  • Your first try just has a missing ) for the parseInt method. Other than that its just fine. Commented Jul 23, 2015 at 15:24
  • Doh! Thanks, accepting the answer shortly Commented Jul 23, 2015 at 15:26
  • What's unclear about "VM4765:2 Uncaught SyntaxError: Unexpected token }"? Commented Jul 23, 2015 at 15:27

4 Answers 4

0

You can do

var arrayOfNumbers = arrayOfStrings.map(Number);

For older browsers which do not support Array.map, you can use Underscore

var arrayOfNumbers = _.map(arrayOfStrings, Number);
Sign up to request clarification or add additional context in comments.

Comments

0

Change it into

var bla = ids_nn.map(function (x) { return parseInt(x, 10)})
VM4765:2 Uncaught SyntaxError: Unexpected token }message: "Unexpected token }"stack: (...)get stack: function () { [native code] }set stack: function () { [native code] }__proto__: ErrorVM3550:847 InjectedScript._evaluateOnVM3550:780 InjectedScript._evaluateAndWrapVM3550:646 InjectedScript.evaluate

Comments

0
var bla = ids_nn.map(function (x) { return parseInt(x, 10})

so close - here's what you should've done

var bla = ids_nn.map(function (x) { return parseInt(x, 10)})

one ) missing

Comments

0

This works for me

var bla = ids_nn.map(function (x) { return parseInt(x) });

You had a mistake with the } and ), but anyway you don't need to pass the base

3 Comments

Thanks, am reading the documentation just now. Don;t get it, why don;t I need to specify decimal with 10?
If the radix parameter is omitted, JavaScript assumes the following: In the parseInt documentation of w3schools I see: If the string begins with "0x", the radix is 16 (hexadecimal) If the string begins with "0", the radix is 8 (octal). This feature is deprecated If the string begins with any other value, the radix is 10 (decimal) w3schools.com/jsref/jsref_parseint.asp
So you only had to specify base if your number starts with 0x prefix like "0x123A" which means you wanted it in hexadecimal

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.