How to use ES6 Fat Arrow to .filter() an array of objects

后端 未结 5 770
感情败类
感情败类 2020-12-04 06:07

I\'m trying to use ES6 arrow function with .filter to return adults (Jack & Jill). It appears I cannot use an if statement.

What do I need to know

5条回答
  •  自闭症患者
    2020-12-04 06:39

    You can't implicitly return with an if, you would need the braces:

    let adults = family.filter(person => { if (person.age > 18) return person} );
    

    It can be simplified though:

    let adults = family.filter(person => person.age > 18);
    

提交回复
热议问题