简体   繁体   English

javascript将数组的元素自己转换为数组

[英]javascript convert elements of an array into array for themselves

How can I convert the elements of the array into another array? 如何将数组的元素转换为另一个数组?

let a = [2, 4, 0, 8, 9, 15]

and the result will be : 结果将是:

a = [[2], [4], [0], [8], [9], [15]]

Use Array#map to iterate the array, and wrap each item in an array: 使用Array#map迭代数组,然后将每个项目包装在数组中:

 let a = [2, 4, 0, 8, 9, 15] const result = a.map((n) => [n]) console.log(JSON.stringify(result)) 

let a = [2, 4, 0, 8, 9, 15];
let b = a.map((item) => [item]);
    a = [2, 4, 0, 8, 9, 15]
    console.log(result = a.map((n) => [n]))
    b=[]
//you can try  below also
     for (i in a){
      b.push([i]);
      }

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

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