How do I find part of an element inside an array? (Javascript)

0

I am making a filter inside an array to get part of the element as it is typed in my search where:

array.filter(row => row.cnpj.indexOf(search) >= 0)

Considering 'search' the search entered, if you have inside the array, cnpj with null value will give error in indexOf:

TypeError: Cannot read property 'indexOf' of null

How do I make this filter by ignoring null values or is there a better way to do this?

    
asked by anonymous 30.01.2018 / 15:56

1 answer

0

I found the answer:

value.filter(row => { 
   const cnpj = row.cnpj || ''
   return cnpj.indexOf(this.search) >= 0
})

It was just putting the value expected by indexOf in a const

    
30.01.2018 / 16:39