I am building a REST API in NodeJS. I am building the server-side pagination, sorting and filtering.
I have a simple implementation of the filter method. This works if the item does not contain any empty strings. However if the item contains an empty string, the .toString()
method fails because item[filterBy]
is null
.
Cannot read property 'toString' of null
How can I update the filter function to simply return true if the item contains an empty string, to skip the comparison operations?
// Apply filter to array of objects
// TODO: Empty strings breaks the filter
items = items.filter(item =>
item[filterBy].toString().trim().toLowerCase().indexOf(filterValue.toLowerCase()) !== -1)