我有以下对象数组:
arr = [
{
name: "john",
age: 24,
gender: "male"
},
{
name: "jane",
age: 27,
gender: "female"
},
{
name: "joe",
age: 29,
gender: "male"
}
]
我正在尝试将name
和age
属性过滤为新数组。我试过这个:
const newFields = arr.filter((item) => {
return (
item.name && item.age
);
});
但由于某种原因newFields
返回一个空数组。
答案 0 :(得分:2)
const arr = [
{
name: "john",
age: 24,
gender: "male"
},
{
name: "jane",
age: 27,
gender: "female"
},
{
name: "joe",
age: 29,
gender: "male"
}
];
const newFields = arr.map(item => {
return {
name: item.name,
age: item.age
}
});
console.log(newFields)