你是一名前端开发,现在有一个大数组,大数组里面有六个小数组,小数组里面有六个对象,现在需要把每个小数组里面的第一个对象拿出来生成一个新数组
时间: 2024-01-21 09:02:56 浏览: 67
好的,针对您的问题,我可以使用 JavaScript 的 map 函数来解决。具体实现如下:
```javascript
const bigArray = [
[{id: 1, name: 'A'}, {id: 2, name: 'B'}, {id: 3, name: 'C'}, {id: 4, name: 'D'}, {id: 5, name: 'E'}, {id: 6, name: 'F'}],
[{id: 7, name: 'G'}, {id: 8, name: 'H'}, {id: 9, name: 'I'}, {id: 10, name: 'J'}, {id: 11, name: 'K'}, {id: 12, name: 'L'}],
[{id: 13, name: 'M'}, {id: 14, name: 'N'}, {id: 15, name: 'O'}, {id: 16, name: 'P'}, {id: 17, name: 'Q'}, {id: 18, name: 'R'}],
[{id: 19, name: 'S'}, {id: 20, name: 'T'}, {id: 21, name: 'U'}, {id: 22, name: 'V'}, {id: 23, name: 'W'}, {id: 24, name: 'X'}],
[{id: 25, name: 'Y'}, {id: 26, name: 'Z'}, {id: 27, name: 'AA'}, {id: 28, name: 'BB'}, {id: 29, name: 'CC'}, {id: 30, name: 'DD'}],
[{id: 31, name: 'EE'}, {id: 32, name: 'FF'}, {id: 33, name: 'GG'}, {id: 34, name: 'HH'}, {id: 35, name: 'II'}, {id: 36, name: 'JJ'}]
];
const newArray = bigArray.map(function(smallArray) {
return smallArray[0];
});
console.log(newArray);
// Output: [{id: 1, name: 'A'}, {id: 7, name: 'G'}, {id: 13, name: 'M'}, {id: 19, name: 'S'}, {id: 25, name: 'Y'}, {id: 31, name: 'EE'}]
```
上面的代码中,我首先定义了一个大数组 `bigArray`,里面包含了六个小数组。然后我使用了 `map` 函数,对 `bigArray` 进行循环遍历,对于每个小数组 `smallArray`,我返回了它的第一个对象,也就是 `smallArray[0]`。最后,`map` 函数将每个小数组的第一个对象组成的新数组返回给了 `newArray` 变量,并打印输出了 `newArray`。
阅读全文