简体   繁体   中英

Convert each element of array into separate array in JavaScript (ES6)

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?

You an use array.map to do that:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM