Javascript 如何使用 ES6 Fat Arrow 来 .filter() 对象数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32040396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:33:15  来源:igfitidea点击:

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

javascriptecmascript-6higher-order-functions

提问by Henry Zhu

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

我正在尝试使用 ES6 箭头函数.filter返回成人(Hyman & Jill)。看来我不能使用 if 语句。

What do I need to know in order to do this in ES6?

为了在 ES6 中做到这一点,我需要知道什么?

var family = [{"name":"Hyman",  "age": 26},
              {"name":"Jill",  "age": 22},
              {"name":"James", "age": 5 },
              {"name":"Jenny", "age": 2 }];

let adults = family.filter(person => if (person.age > 18) person); // throws error

(8:37) SyntaxError: unknown: Unexpected token (8:37)
|let adults = family.filter(person => if (person.age > 18) person);

My working ES5 example:

我的工作 ES5 示例:

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

回答by Felix Kling

It appears I cannot use an if statement.

看来我不能使用 if 语句。

Arrow functions either allow to use an expressionora blockas their body. Passing an expression

箭头功能或者允许使用一个表达式作为他们的身体。传递表达式

foo => bar

is equivalent to the following block

相当于下面的块

foo => { return bar; }

However,

然而,

if (person.age > 18) person

is not an expression, ifis a statement. Hence you would have to use a block, if you wanted to use ifin an arrow function:

不是表达式,if是语句。因此,如果要if在箭头函数中使用,则必须使用块:

foo => {  if (person.age > 18) return person; }


While that technically solves the problem, this a confusing use of .filter, because it suggests that you have to return the value that should be contained in the output array. However, the callback passed to .filtershould return a Boolean, i.e. trueor false, indicating whether the element should be included in the new array or not.

虽然这在技术上解决了问题,但这是一个令人困惑的用法.filter,因为它表明您必须返回应该包含在输出数组中的值。但是,传递给的回调.filter应该返回一个Boolean,即trueor false,指示该元素是否应包含在新数组中。

So all you need is

所以你只需要

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

In ES5:

在 ES5 中:

family.filter(function (person) {
  return person.age > 18;
});

回答by Kit Sunde

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

你不能用 an 隐式返回if,你需要大括号:

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

It can be simplified though:

不过可以简化一下:

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

回答by Mo.

As simple as you can use const adults = family.filter(({ age }) => age > 18 );

尽可能简单地使用 const adults = family.filter(({ age }) => age > 18 );

const family =[{"name":"Hyman",  "age": 26},
              {"name":"Jill",  "age": 22},
              {"name":"James", "age": 5 },
              {"name":"Jenny", "age": 2 }];

const adults = family.filter(({ age }) => age > 18 );

console.log(adults)

回答by Sabri Mevi?

Here is my solution for those who use hook; If you are listing items in your grid and want to remove the selected item, you can use this solution.

这是我为那些使用的人的解决方案hook;如果您在网格中列出项目并希望删除所选项目,则可以使用此解决方案。

var list = data.filter(form => form.id !== selectedRowDataId);
setData(list);