Convert string to multiple arrays in JavaScript

Viewed 561

I have a comma separated string and i want to convert it into multiple arrays. Here is the code i tried but failed to achieve the expected output.

const data = "123,456";
result = data.split(',').map(s => Array.from(s));

console.log(result); // [["1", "2", "3"], ["4", "5", "6"]]

Expected output:

[["123"], ["456"]]
2 Answers

Map it to an array.

const data = "123,456";
result = data.split(',').map(s => [s]);

console.log(result);

As epascarello suggests on his comment, try

data.split(',').map(s =>[s]);

Take a look here for more info!

Related