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
Community
  • 1
  • 1
Doug Fir
  • 19,971
  • 47
  • 169
  • 299

4 Answers4

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
adamliesko
  • 1,887
  • 1
  • 14
  • 21
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

Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
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

Manjar
  • 3,159
  • 32
  • 44
  • Thanks, am reading the documentation just now. Don;t get it, why don;t I need to specify decimal with 10? – Doug Fir Jul 23 '15 at 15:29
  • 1
    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) http://www.w3schools.com/jsref/jsref_parseint.asp – Manjar Jul 23 '15 at 15:32
  • So you only had to specify base if your number starts with 0x prefix like "0x123A" which means you wanted it in hexadecimal – Manjar Jul 23 '15 at 15:33
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);
Jonas Anseeuw
  • 3,590
  • 5
  • 22
  • 23