Home > Software engineering >  Convert each element of array into separate array in JavaScript (ES6)
Convert each element of array into separate array in JavaScript (ES6)

Time:06-09

Convert each element of array into separate array and push into one array.

inputArray = ['One', 'Two', 'Three']

Required Output

outputArray = [['One'],['Two'],['Three']]

By using ES6 how to get this output?

CodePudding user response:

You an use array.map to do that:

const inputArray = ['One', 'Two', 'Three'];
const result = inputArray.map(x => [x]);
console.log(result);

CodePudding user response:

You mean a simple map of each element? That can be done very quickly like so

const outputArray = inputArray.map(el => [el]);
  • Related