0

I've got a filter method that check if result doesn't contain value from this.state.filter it should be delete.

My problem is method doesn't remove wrong result. It makes null

filter(){
    const {fetchData} = this.state;
  return  fetchData.map((result, index) => (
      (result[3] == this.state.filter) ?
      result :  null //there is my problem
    ))
  }

screens from console.log

before

after

EnzyWeiss
  • 198
  • 1
  • 2
  • 12
  • `fetchData` is an array ? – Dane Oct 15 '17 at 11:00
  • Yep, it is an array which contains other arrays – EnzyWeiss Oct 15 '17 at 11:00
  • 1
    You should probably give an example of your `fetchData` input, and how the function is called. And if `result[3]` is an array then it won't be equal to another array, you can check if the array contains the same values though, or the same values in the same order. Take a look at "[How to compare arrays in JavaScript?](https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript)" – David Thomas Oct 15 '17 at 11:01

1 Answers1

4

Instead of map you must be using Array.prototype.filter to remove elements from array:

filter() {
  const { fetchData } = this.state;
  return fetchData.filter( (subarray) => (
    subarray[3] === this.state.filter
  ));
}
Dane
  • 9,242
  • 5
  • 33
  • 56