0

I have an array like ["1,2,3,4"]. I am simply trying to convert the numbers inside this array to a pure integer array like [1,2,3,4].

This is what I have tried so far...

var a = ["1,2,3,4"];
var b = a.map(val => val.split(','));
console.log(b); // prints ["1", "2", "3", "4"]
var c = b.map(x => parseInt(x));
console.log(c); // prints [1]

Why is c only prints [1], instead of looping through all the elements inside b array and print [1,2,3,4]?

Slyper
  • 896
  • 2
  • 15
  • 32
  • why do you have array with a string inside? could you have more strings inside? which result do you expect in this case? – Nina Scholz Jul 28 '19 at 10:15

5 Answers5

3

Use split() as below

var a = ["1,2,3,4"];
var b = a[0].split(",").map(x=>parseInt(x));
console.log(b);
Harun Or Rashid
  • 5,589
  • 1
  • 19
  • 21
1

console.log(b); // prints ["1", "2", "3", "4"]

This is not correct, it actually prints [["1", "2", "3", "4"]] (a 2d array) as you are mapping a string in an array to an array, thus, you still maintain the outer array.

Instead, you can use .flapMap() to map the contents of the array that .split() returns to the outer container array like so:

var a = ["1,2,3,4"];
var b = a.flatMap(val => val.split(','));
console.log(b); // prints ["1", "2", "3", "4"]
var c = b.map(x => parseInt(x));
console.log(c); // prints [1, 2, 3, 4]

Alternatively, you can also use JSON.parse() on your string of arrays with .flatMap to get an array of numbers like so:

var a = ["1,2,3,4"];
var b = a.flatMap(val => JSON.parse(`[${val}]`));
console.log(b); // [1, 2, 3, 4]

However, do note, .flatMap() does currently have limited browser support, and so, instead, you may want to use .reduce() if you have multiple elements:

var a = ["1,2,3,4"];
var b = a.reduce((acc, val) => [...acc, ...val.split(',')], []);
console.log(b); // prints [["1", "2", "3", "4"]]
var c = b.map(x => parseInt(x));
console.log(c); // prints [1, 2, 3, 4]

The advantage of using these two (three) methods is that it will work for a values where you have multiple beginning strings such as ["1,2,3,4", "5,6,7,8"] etc...

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • 1
    Hi @NickParsons, Thanks for taking time to explain the problem in my code, and then explaining your solution. Much appreciated. – Slyper Jul 28 '19 at 10:20
0

You should do something like that:

var a = "1,2,3,4";
var b = a.split(',') // or a[0].split(",") if you really want to use ["1,2,3,4"]
var c = b.map(x => parseInt(x));
console.log(c);
Emanuele Scarabattoli
  • 4,103
  • 1
  • 12
  • 21
0

You can use + for implicit conversion of string to number

var a = ["1,2,3,4"];
var b = a[0].split(',').map(v=> +v);
console.log(b);
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
0

You create a nested array by taking an array of a string. This returns an array of arrays.

To overcome nested arrays, you fould flat them in a first step and then map with Number as callback

var a = ["1,2,3,4"];
var b = a.flatMap(val => val.split(',')).map(Number);
console.log(b); // [1, 2, 3, 4]
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392