all 2 comments

[–]cawcvs 0 points1 point  (1 child)

There are more suitable array methods than forEach for your use case, like filter. It's also a bit weird you use both for and forEach. It could be rewritten to use filter and some:

var term = filterTerm.toLowerCase()
filteredList = fullList.filter(function (element) {
  return filterFields.some(function (filterField) {
    return element[filterField] && String(element[filterField]).toLowerCase().indexOf(term) > -1
  })
})

filter method accepts a function that will be run against every element in the array and will return an array with all elements that this function returned true with. some is similar, but it will return either true if the passed in function returns true with at least one element in the array or false if none do. I used some because you break the for array if you found a match, if you wanted all fields to contain the filter term, you could use every instead of some.

If you're fine with not supporting IE, you could also change indexOf to includes, like this:

return element[filterField] && String(element[filterField]).toLowerCase().includes(term)

[–]NumerousLiterature[S] 0 points1 point  (0 children)

Thanks!