Categories
JavaScript Answers

How to search array of arrays with JavaScript?

Spread the love

To search array of arrays with JavaScript, we use the array some and every methods.

For instance, we write

const ar = [
  [2, 6, 89, 45],
  [3, 566, 23, 79],
  [434, 677, 9, 23],
];

const val = [3, 566, 23, 79];

const bool = ar.some((arr) => {
  return arr.every((prop, index) => {
    return val[index] === prop;
  });
});

to call some with a callback that searches every element of the nested array to check if they’re equal with every.

We call every with a callback that check the index of arr to see if it’s equal to prop in the nested array.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *