Javascript 如何映射对象数组并从对象返回特定属性
我有一个具有以下格式的对象数组:Javascript 如何映射对象数组并从对象返回特定属性,javascript,Javascript,我有一个具有以下格式的对象数组: [ { "team_key": "2611", "team_name": "Leicester", "team_badge": "https://apiv2.apifootball.com/badges/2611_leicester.png", "founded&
[
{
"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"
},
]
我想映射数组并返回一个新数组,其中只包含特定参数,如“team_name”和“founded”
新阵列应如下所示:
[
{
"team_name": "Leicester",
"founded": "1884; 136 years ago (as Leicester Fosse FC)",
},
{
"team_name": "Everton",
"founded": "1878; 142 years ago",
}
]
映射数组
const数据=[
{
“团队密钥”:“2611”,
“团队名称”:“莱斯特”,
“团队徽章”:https://apiv2.apifootball.com/badges/2611_leicester.png",
“成立”:“1884年;136年前(作为莱斯特·福斯俱乐部)”,
“城市”:“莱斯特”
},
{
“团队密钥”:“2612”,
“球队名称”:“埃弗顿”,
“团队徽章”:https://apiv2.apifootball.com/badges/2612_everton.png",
“成立”:“1878年;142年前”,
“城市”:“利物浦”
},
]
console.log(
//将对象参数分解为所需的键
//并从中返回一个新对象。
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是您的新阵列
如果我理解错了问题,或者您希望得到其他答案,请通过留言或回复告知我,我会尽力解决。您已经尝试过的问题是什么?