假设我有一个数组常量,如下所示:
const people = [
{ first: 'John', last: 'Doe', year: 1991, month: 6 },
{ first: 'Jane', last: 'Doe', year: 1990, month: 9 },
{ first: 'Jahn', last: 'Deo', year: 1986, month: 1 },
{ first: 'Jone', last: 'Deo', year: 1992, month: 11 },
{ first: 'Jhan', last: 'Doe', year: 1989, month: 4 },
{ first: 'Jeon', last: 'Doe', year: 1992, month: 2 },
{ first: 'Janh', last: 'Edo', year: 1984, month: 7 },
{ first: 'Jean', last: 'Edo', year: 1981, month: 8},
];
而我想返回一个每个80后的值。
我目前实现这一目标的工作职能是:
const eighty = people.filter(person=> {
if (person.year >= 1980 && person.year <= 1989) {
return true;
}
});
我的问题: 是否可以使用 startsWith() 和 filter() 来替换:
if (person.year >= 1980 && person.year <= 1989) {
return true;
}
用 startsWith('198')
代替?
如果是,那么正确的方法是什么?
原文由 AndrewL64 发布,翻译遵循 CC BY-SA 4.0 许可协议
你可以做