Skip to content Skip to sidebar Skip to footer

Is There A Javascript Function For Checking If An Array Of Arrays Contains A Specific Array?

I'm making a Javascript application where I need to check an array of arrays (a nested array) for specific arrays containing two objects each. The nested array looks somewhat like

Solution 1:

In JS each array(object) is unique, even if it has the same content as another one:

console.log( {} == {} ); // false
console.log( [] == [] ); // false
console.log( [1, 2] == [1, 2] ); // false

So, you have to loop and compare each element one by one:

let arr = [
  [0, 0, 0], 
  [1, 1, 1],
  [2, 2, 2], 
]

console.log( arrInArr( arr, [3, 3, 3] ) ); // false
arr[3] = [3, 3, 3]; // new elem added
console.log( arrInArr( arr, [3, 3, 3] ) ); // true

function arrInArr(mama, child){
  for( let i = 0; i < mama.length; i++ ){
    // don't even starting to compare if length are not equal
    if( mama[i].length != child.length ){ continue }
    
    let match = 0; // To count each exact match
    for( let j = 0; j < child.length; j++ ){
      if( mama[i][j] === child[j] ){
        // We made sure, that they have equal length, and can use index 'j'
        // to compare each element of arrays. 
        match++;
      } else {
        break;
      }
    }
    if( match === child.length ){ // If each element exactly matched
      return true; 
    }
  }
  return false;
}

Solution 2:

includes compares with ===, apparently your search array is only equivalent to one of the entries, not the same actual array.

You can use some with the callback checking whether every entry in the array being visited matches your search array.

const flag = arrayOfArrays.some(
    array => array.length === search.length && array.every(
        (entry, index) => entry === search[index]
   )
);

Note that this assumes the inner entries are comparable with ===. If not, substitute the appropriate way of comparing them.

Live Example:

function check(arrayOfArrays, search) {
    return arrayOfArrays.some(
        array => array.length === search.length && array.every(
            (entry, index) => entry === search[index]
       )
    );
}
console.log(
    "yes:", 
    check(
        [[1, 2], [3, 4], [5, 6], [7, 8]],
        [5, 6]
    )
);
console.log(
    "no:", 
    check(
        [[1, 2], [3, 4], [5, 6], [7, 8]],
        [6, 5] // Note the different order
    )
);

If you want to find the array in the array of arrays, you can use find instead of some. If you want to find its index in the array of arrays, you can use findIndex.


Solution 3:

If you use a package like lodash, it has a method for comparing if two objects are equal by content rather than by the much stricter === comparison: https://lodash.com/docs/4.17.15#isEqual

Using _.isEqual, you could then do something like this:

array.findIndex(item => _.isEqual(item, testObject)) >= 0

Lodash might even have a method to directly search an array with a deep-equals comparison too, but the above would get the job done.


Post a Comment for "Is There A Javascript Function For Checking If An Array Of Arrays Contains A Specific Array?"