有人知道怎么写这段代码吗?单击按钮后,从people表创建一个新表,该表将只包含年龄大于18岁的人。在控制台中显示结果数组
const people = [
{
firstname: "Dreddy",
lastname: "Nussgen",
age: 70,
},
{
firstname: "Yves",
lastname: "Sear",
age: 49,
},
{
firstname: "Marcel",
lastname: "Cowderay",
age: 59,
},
{
firstname: "Dag",
lastname: "Binnall",
age: 31,
},
{
firstname: "Horten",
lastname: "Claesens",
age: 75,
},
{
firstname: "Charmian",
lastname: "Harnes",
age: 10,
},
{
firstname: "Sean",
lastname: "Littlejohns",
age: 37,
},
{
firstname: "Hazlett",
lastname: "Sprouls",
age: 87,
},
{
firstname: "Marcel",
lastname: "Hasted",
age: 66,
},
{
firstname: "Cary",
lastname: "Summerson",
age: 15,
},
{
firstname: "Feodor",
lastname: "Ollett",
age: 56,
},
{
firstname: "Kelly",
lastname: "Ranahan",
age: 62,
},
{
firstname: "Madelene",
lastname: "Davie",
age: 14,
},
{
firstname: "Bent",
lastname: "Karpe",
age: 82,
},
{
firstname: "Reinaldo",
lastname: "Grimbleby",
age: 81,
},
];
可以将Array.prototype.filter()与Destructuring赋值结合使用
代码:
const people = [{firstname: 'Dreddy',lastname: 'Nussgen',age: 70,},{firstname: 'Yves',lastname: 'Sear',age: 49,},{firstname: 'Marcel',lastname: 'Cowderay',age: 59,},{firstname: 'Dag',lastname: 'Binnall',age: 31,},{firstname: 'Horten',lastname: 'Claesens',age: 75,},{firstname: 'Charmian',lastname: 'Harnes',age: 10,},{firstname: 'Sean',lastname: 'Littlejohns',age: 37,},{firstname: 'Hazlett',lastname: 'Sprouls',age: 87,},{firstname: 'Marcel',lastname: 'Hasted',age: 66,},{firstname: 'Cary',lastname: 'Summerson',age: 15,},{firstname: 'Feodor',lastname: 'Ollett',age: 56,},{firstname: 'Kelly',lastname: 'Ranahan',age: 62,},{firstname: 'Madelene',lastname: 'Davie',age: 14,},{firstname: 'Bent',lastname: 'Karpe',age: 82,},{firstname: 'Reinaldo',lastname: 'Grimbleby',age: 81,},]
const result = people.filter(({ age }) => age > 18)
console.log(result)