遍历数组中的对象数组

时间:2020-09-14 12:25:04

标签: javascript

我有下一个数据:

const someArr = [
[{id:1, value: 'test1'}, {id:2, value: 'test2'}],
[{id:3, value: 'test3'}, {id:4, value: 'test4'}],
[{id:5, value: 'test5'}, {id:6, value: 'test6'}]
];

通过此方法迭代并获取每个ID和值的最佳方法是什么?在map()中使用map()吗?

someArr.map(items => items.map(item => console.log(item)));

1 个答案:

答案 0 :(得分:1)

您可以使用flatMap展平结构,然后遍历数据。

const someArr = [
[{id:1, value: 'test1'}, {id:2, value: 'test2'}],
[{id:3, value: 'test3'}, {id:4, value: 'test4'}],
[{id:5, value: 'test5'}, {id:6, value: 'test6'}]
];

someArr.flatMap(x => x).forEach(x => console.log(x))