Home  >  Q&A  >  body text

How to filter nested arrays in React?

<p>I have an array of arrays in my React app that I need to filter based on the first column. </p> <p>The first column contains the list of companies. </p> <pre class="brush:php;toolbar:false;">const rowData = [ ['Capri LLC', '0012345', 'A0012', 'Y', 'View Details'], ['Capricorn INC', '0022345', 'B0012', 'N', 'View Details'], ['Cancer INC', '0033345', 'A0012', 'Y', 'View Details'], ['Gemini LLC', '0052345', 'C0012', 'Y', 'View Details'], ['Leo Land INC', '0052345', 'D0012', 'Y', 'View Details'], ['Capri LLC', '0012345', 'A0012', 'Y', 'View Details'], ['Capricorn INC', '0022345', 'B0012', 'N', 'View Details'], ['Cancer INC', '0033345', 'A0012', 'Y', 'View Details'], ['Gemini LLC', '0052345', 'C0012', 'Y', 'View Details'], ['Leo Land INC', '0052345', 'D0012', 'Y', 'View Details'], ];</pre> <p>So if I search for Capri, the final output should be: </p> <pre class="brush:php;toolbar:false;">const rowData = [ ['Capri LLC', '0012345', 'A0012', 'Y', 'View Details'], ['Capricorn INC', '0022345', 'B0012', 'N', 'View Details'], ['Capri LLC', '0012345', 'A0012', 'Y', 'View Details'], ['Capricorn INC', '0022345', 'B0012', 'N', 'View Details'], ];</pre> <p>I tried applying the .filter() method we use for one-dimensional arrays, but I made some syntax errors when applying to an array of arrays. </p>
P粉022501495P粉022501495403 days ago411

reply all(1)I'll reply

  • P粉124070451

    P粉1240704512023-08-16 11:04:04

    Try this:

    rowData.filter(a => a[0].includes('Capri'));

    reply
    0
  • Cancelreply