How is () => {…} different from () =>

  • A+
Category:Languages

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.

Weirdly, this doesn't work

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

while this does

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.

You have to write it down :

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

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: