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

时间:2018-01-20 19:17:14

标签: javascript arrays

如何将数组元素转换为另一个数组?

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

,结果将是:

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

3 个答案:

答案 0 :(得分:7)

使用Array#map迭代数组,并将每个项目包装在一个数组中:



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

const result = a.map((n) => [n])

console.log(JSON.stringify(result))




答案 1 :(得分:1)

let a = [2, 4, 0, 8, 9, 15];
let b = a.map((item) => [item]);

答案 2 :(得分:1)

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