[英]How can i map array of objects and return specific properties from the objects
I have an array of objects with this format:我有一个具有这种格式的对象数组:
[
{
"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"
},
]
I want to map the array and return a new array with only specific parameters like "team_name" and "founded".我想 map 数组并返回一个新数组,其中只有特定参数,如“team_name”和“founded”。
The new array should look like this:新数组应如下所示:
[
{
"team_name": "Leicester",
"founded": "1884; 136 years ago (as Leicester Fosse FC)",
},
{
"team_name": "Everton",
"founded": "1878; 142 years ago",
}
]
Map the array? 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 })) )
according to your questing, let's assume, the initial array is x.根据您的要求,我们假设初始数组是 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"
},
] ]
now you want to remove some items from that array-objects,现在您想从该数组对象中删除一些项目,
y = x.map ( v => {
return {
team_name: v.team_name,
founded: v.founded
}
})
more short hand answer:更简短的答案:
y = x.map(({ team_key, founded }) => ({ team_key, founded }))
now y is your new array.现在 y 是你的新数组。
if I understood the question wrong or you expect some other answer, please let me know by message or reply, will try best to solve it.如果我对问题的理解有误或您希望得到其他答案,请通过消息或回复告诉我,我们会尽力解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.