javascript convert elements of an array into array for themselves

前端 未结 3 1134
醉话见心
醉话见心 2021-01-25 21:11

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],          


        
相关标签:
3条回答
  • 2021-01-25 21:28

    Use Array#map to iterate the array, and wrap each item in an array:

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

    0 讨论(0)
  • 2021-01-25 21:46
    let a = [2, 4, 0, 8, 9, 15];
    let b = a.map((item) => [item]);
    
    0 讨论(0)
  • 2021-01-25 21:46
        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]);
          }
    
    0 讨论(0)
提交回复
热议问题