我无法弄清楚如何为这个问题找出解决方案......以及想法?
检查每个人的年龄是否为数组。如果是,则将每个数字值相加并将年龄更新为此值。然后,根据此更新的号码列出仅年龄超过25岁的人员。
const ListOfPeople = [
{
name: 'Mia',
age: [10, 5, 5]
},
{
name: 'Steve',
age: 30
},
{
name: 'Emma',
age: [2, 12, 16, 10]
}
];
我从这开始:
let filterPeople = ListOfPeople => ListOfPeople.map(person =>
person[0].age //stuck
);
答案 0 :(得分:3)
尝试这样的事情:
const over30 = ListOfPeople.map(p => {
if (Array.isArray(p.age)) {
// Copy the object so we don't mutate the original
p = Object.assign({}, p);
p.age = p.age.reduce((sum, v) => sum + v, 0);
}
return p;
}).filter(p => p.age >= 30);
console.log(over30);