怎么是()=> {...}与()=>不同 [英] How is () => {...} different from () =>

查看:130
本文介绍了怎么是()=> {...}与()=>不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一个奇怪的问题。

I've found a weird issue.

给定一个过滤器和一个对象数组,我想只选择那些与过滤器匹配的对象。

given a filter and an array of objects, I would like to select only those objects that match the filter.

奇怪的是,这不起作用

this.state.articles.filter((article) => {
  article.category === filter 
})

虽然这样做

this.state.articles.filter((article) => article.category === filter )

我原本以为他们会评价相同,但似乎不一样是这样的。有什么想法吗?

I originally thought they would evaluate the same, but it doesn't seem to be the case. Any ideas why?

推荐答案

当你打开一个块 {} 时箭头函数,返回不再暗示。

When you open a block {} in an arrow function, the return isn't implied anymore.

你必须把它写下来:

this.state.articles.filter((article) => {
  return article.category === filter 
})

这篇关于怎么是()=> {...}与()=>不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆