Why does my filter not work when i add intergers?

I try to show and hide elements from an array. The entire filter works well except with the numbers. When I use this filter that item.sprint === does not grab this.state.currentSprint and just displays both:

{   this.state.personalItems   .filter(     item => item.user == this.state.user      || item.public == "true"      && item.sprint === this.state.currentSprint   )   .map((l, i) => ()) } 

This variable corresponds to 1 === 2 as in the following example:

{   this.state.personalItems     .filter(       item => item.user == this.state.user        || item.public == "true"        && 1 === 2     )     .map((l, i) => ()) } 

I have already checked if they are integers etc. does anyone have the solution where it goes wrong?

Add Comment
1 Answer(s)

Check operator precedence for && and ||. In your code it will evaluate condition as below.

item => item.user == this.state.user      || (item.public == "true" && item.sprint === this.state.currentSprint) 

You should use wrap || condition with () and update condition as below.

{   this.state.personalItems   .filter(     item => (item.user == this.state.user || item.public == "true")             && item.sprint === this.state.currentSprint   )   .map((l, i) => ()) } 
Answered on July 16, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.