ES6:如何转换和数组数组

时间:2016-10-05 14:39:23

标签: javascript arrays ecmascript-6

我有一个数组数组:

let theArrayOfArrays = [
  [3,7,8],
  [2,0,9],
  [1,7,5]
];

我想重新映射那个数组数组,这样0索引处的所有数字都是一个数组,在1索引处是另一个数组......就像这样:

let theArrayOfArrays = [
  [3,2,1],
  [7,0,7],
  [8,9,5]
];

这就是我现在正在做的事情(似乎应该有更好的方法)。

   //I know all of the sub arrays are the same length 
   //so I just take the first one and make an array of empty arrays:
   let newArr = [];
   theArrayOfArrays[0].map((num, index)=>newArr[index]=[]);

   //now newArr is an array of 3 empty sub arrays
   //I then map though the original array of arrays and fill in newArr
   theArrayOfArrays.map(subArr => subArr.map( (num, index) =>  newArr[index].push(num) ) );

0 个答案:

没有答案