【发布时间】:2018-11-05 03:29:48
【问题描述】:
考虑到下面的数组,其中包含多个对象,我试图在一个数组中获取特定值。说白了,我想提取“红”队中的人的名字。
//
const array = [
{
username: "Mike",
team: "red",
score: 20,
items: ["bat", "book", "gloves"]
},
{
username: "Moe",
team: "blue",
score: 30,
items: ["cellphone", "backpack", "cards"]
},
{
username: "Ellie",
team: "red",
score: 15,
items: ["ball", "canteen", "crayon"]
},
{
username: "little_joe",
team: "green",
score: 1,
items: ["book", "pencil"]
},
];
//using filter method :
const filterArray=array.filter((num)=>{ if (num.team==="red" ){ return num.username}});
console.log(filterArray);
//result : an array including two objects full of unwanted values.
我应该怎么做才能得到一个只有所需值的数组(而不是两个大对象)?
我正在尝试获取这样的数组: (2)►[迈克,艾莉]
【问题讨论】:
标签: javascript arrays object filter