const myFavList = [
{ id: 23, name: "moketaro", favs :[ { fruits: "Apple" }, { hoby: "reading" } ] },
{ id: 11, name: "mokeko", favs :[ { fruits: "lemon" } ] },
{ id: 65, name: "mokemasa", favs :[ { fruits: "orange" }, { hoby: "birdwatching" } ] },
{ id: 190, name: "mokena", favs :[ { fruits: "kiwi" }, { hoby: "travel" } ] },
]
let resFiltered = myFavList.map(({name, favs}) => ({
name: name,
fruits: favs.find(fav => fav.fruits)?.fruits
}));
console.log(resFiltered);
输出:
[{
fruits: "Apple",
name: "moketaro"
}, {
fruits: "lemon",
name: "mokeko"
}, {
fruits: "orange",
name: "mokemasa"
}, {
fruits: "kiwi",
name: "mokena"
}]
使用的语法
- 映射()方法
- 解构
- 箭头函数
- 可选的链接运算符 (?.)
评论(0)