[英]How can i map array of objects and return specific properties from the objects
我有一個具有這種格式的對象數組:
[
{
"team_key": "2611",
"team_name": "Leicester",
"team_badge": "https://apiv2.apifootball.com/badges/2611_leicester.png",
"founded": "1884; 136 years ago (as Leicester Fosse FC)",
"city": "Leicester"
},
{
"team_key": "2612",
"team_name": "Everton",
"team_badge": "https://apiv2.apifootball.com/badges/2612_everton.png",
"founded": "1878; 142 years ago",
"city": "Liverpool"
},
]
我想 map 數組並返回一個新數組,其中只有特定參數,如“team_name”和“founded”。
新數組應如下所示:
[
{
"team_name": "Leicester",
"founded": "1884; 136 years ago (as Leicester Fosse FC)",
},
{
"team_name": "Everton",
"founded": "1878; 142 years ago",
}
]
Map 陣列?
const data = [ { "team_key": "2611", "team_name": "Leicester", "team_badge": "https://apiv2.apifootball.com/badges/2611_leicester.png", "founded": "1884; 136 years ago (as Leicester Fosse FC)", "city": "Leicester" }, { "team_key": "2612", "team_name": "Everton", "team_badge": "https://apiv2.apifootball.com/badges/2612_everton.png", "founded": "1878; 142 years ago", "city": "Liverpool" }, ] console.log( // Destruct the object argument into desired keys // and return a new object from them. data.map(({ team_key, founded }) => ({ team_key, founded })) )
根據您的要求,我們假設初始數組是 x。
x = [
{
"team_key": "2611",
"team_name": "Leicester",
"team_badge": "https://apiv2.apifootball.com/badges/2611_leicester.png",
"founded": "1884; 136 years ago (as Leicester Fosse FC)",
"city": "Leicester"
},
{
"team_key": "2612",
"team_name": "Everton",
"team_badge": "https://apiv2.apifootball.com/badges/2612_everton.png",
"founded": "1878; 142 years ago",
"city": "Liverpool"
},
]
現在您想從該數組對象中刪除一些項目,
y = x.map ( v => {
return {
team_name: v.team_name,
founded: v.founded
}
})
更簡短的答案:
y = x.map(({ team_key, founded }) => ({ team_key, founded }))
現在 y 是你的新數組。
如果我對問題的理解有誤或您希望得到其他答案,請通過消息或回復告訴我,我們會盡力解決。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.