gpt4 book ai didi

javascript - 在 es6 中使用 map 映射嵌套数组

转载 作者:行者123 更新时间:2023-11-30 09:24:00 25 4
gpt4 key购买 nike

我正在尝试使用 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 } ]​​​​​

谁能帮我映射嵌套数组?

最佳答案

尝试跟随

const results = [
{
id: 1,
details: [
{
color: "red",
}
]
},
{
id: 2,
details: [
{
color: "blue",
}
]
}]

const list1 = results.map(car => {
return car.details;
});

// assuming there was a typo here it should be list1
const list2 = list1.map(details => {
return {
detail: details[0].color // details is an array
}
});

console.log(list1);
console.log(list2);

关于javascript - 在 es6 中使用 map 映射嵌套数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49914241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com