mike23
mike23

Reputation: 21

Trying to filter table where one row is an array

I'm building a searchbox for the filtering table using react-table. The table has a few properties, and one of them is an array.

Searchbox works pretty fine for each one row (name, id, label), but it crashes when I'm trying to search for the one property, which comes from array (colors).

Below is my search function. I'm getting the value from the input field.

function search(rows: any[]) {
    return rows.filter(
      (row) => 
      row.name.toLowerCase().indexOf(value) > -1 ||
      row.id.toLowerCase().indexOf(value) > -1 ||
      row.label.toLowerCase().indexOf(value) > -1 ||
      **row.colors.toLowerCase().indexOf(value) > -1** <- that doesn't work
      )
  };

It'd be great, if anyone could help me.

Cheers!

Upvotes: 1

Views: 86

Answers (1)

buzatto
buzatto

Reputation: 10382

given colors is array you must iterate over and validate if some pass for the given condition:

function search(rows: any[]) {
    return rows.filter(
      (row) => 
      row.name.toLowerCase().indexOf(value) > -1 ||
      row.id.toLowerCase().indexOf(value) > -1 ||
      row.label.toLowerCase().indexOf(value) > -1 ||
      row.colors.some(color => color.toLowerCase().indexOf(value) > -1)
      )
  };

reference: Array.prototype.some

Upvotes: 1

Related Questions