简体   繁体   English

在 JavaScript (ES6) 中将数组的每个元素转换为单独的数组

[英]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?通过使用 ES6 如何获得这个输出?

You an use array.map to do that:您可以使用array.map来做到这一点:

 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]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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