【发布时间】:2018-09-29 13:17:17
【问题描述】:
我正在尝试使用 map 遍历嵌套数组。
const results = [
{
id: 1,
details: [
{
color: "red",
}
]
},
{
id: 2,
details: [
{
color: "blue",
}
]
}]
const list1 = results.map(car => {
return car.details;
})
const list2 = list.map(details => {
return {
detail: details.color
}
});
console.log(list1);
console.log(list2);
List1 显示正常:
[ [ { color: 'red' } ], [ { color: 'blue' } ] ]
但是使用 list2 我得到以下信息:
[ { detail: undefined }, { detail: undefined } ]
谁能帮我映射嵌套数组?
【问题讨论】:
-
你期望这是什么
[ { detail: undefined }, { detail: undefined } ]? -
你使用的是list.map,应该是list1.map(..., for list2
-
list是什么? -
使用应该使用
list1而不是list。
标签: javascript arrays object ecmascript-6