JDLR
JDLR

Reputation: 600

Create multi array from array in Javascript

i got an array of numbers [12345,345653,456356]but i need to convert this to something like [[1232131],[234324],[67657]]for using a CSV react component tool that reads data using this format. Do you got some idea how to make this? i tried this using push but i dont get the correct format.

Upvotes: 1

Views: 34

Answers (2)

kosmičák
kosmičák

Reputation: 1043

Not as elegant as mickl's map() but if you prefer an oldfashioned loop:

let input = [ 1, 2, 3 ];
let output = [];

for (let i = 0; i < input.length; i++) {
  output.push([ input[i] ]);
} 

console.log(output);

Upvotes: 0

mickl
mickl

Reputation: 49945

You can simply use .map() to create a new array based on existing one:

let input = [12345,345653,456356];
let output = input.map(x => [x]);
console.log(output);

Upvotes: 2

Related Questions