Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an array contains another array?

I needed 2d arrays, so I made a nested array since JavaScript doesn't allow them.

They look like this:

var myArray = [
  [1, 0],
  [1, 1],
  [1, 3],
  [2, 4]
]

How can I check if this array includes a specific element (i.e. one of these [0,1] arrays) in vanilla JS?

Here is what I tried, with no success (everything returns false) (EDIT: I included the answers in the snippet):

var myArray = [
  [1, 0],
  [1, 1],
  [1, 3],
  [2, 4]
]

var itemTrue = [2, 4];
var itemFalse = [4, 4];

function contains(a, obj) {
  var i = a.length;
  while (i--) {
    if (a[i] === obj) {
      return true;
    }
  }
  return false;
}

// EDIT: first answer's solution

function isArrayInArray(x, check) {
  for (var i = 0, len = x.length; i < len; i++) {
    if (x[i][0] === check[0] && x[i][1] === check[1]) {
      return true;
    }
  }
  return false;
}

// EDIT: accepted answer's solution


function isArrayInArray2(x, check) {
  var result = x.find(function(ele) {
    return (JSON.stringify(ele) === JSON.stringify(check));
  }) 
  return result !=null
}

console.log("true :" + myArray.includes(itemTrue));
console.log("false :" + myArray.includes(itemFalse));

console.log("true :" + (myArray.indexOf(itemTrue) != -1));
console.log("false :" + (myArray.indexOf(itemFalse) != -1));

console.log("true :" + contains(myArray, itemTrue));
console.log("false :" + contains(myArray, itemFalse));

// EDIT: first answer's solution
console.log("true :" + isArrayInArray(myArray, itemTrue));
console.log("false :" + isArrayInArray(myArray, itemFalse));


// EDIT: accepted answer's solution
console.log("true :" + isArrayInArray2(myArray, itemTrue));
console.log("false :" + isArrayInArray2(myArray, itemFalse));

It could look like duplicate but I couldn't find a similar question. If it is, feel free to tag it as such.

like image 862
Billybobbonnet Avatar asked Jan 15 '17 12:01

Billybobbonnet


People also ask

How do I check if an array exists in another array?

Loop through second array and check if elements in the second array exists on created object. If element exist then return true else return false. Use the inbuilt ES6 function some () to iterate through each and every element of first array and to test the array.

How to check if an array contains every element in it?

You could check that the larger of the arrays outercontains every element in the smaller one, i.e. inner: public static boolean linearIn(Integer[] outer, Integer[] inner) { return Arrays.asList(outer).containsAll(Arrays.asList(inner)); } Note: Integertypes are required for this approach to work.

How to check if two arrays share the same elements?

Since sets cannot have duplicate elements while arrays can, combining both input arrays, converting it to a set, and comparing the set size and array length would tell you if they share any elements. Show activity on this post. When I looked at your answers, I could not find the answer I wanted.

How do you check if an array has multiple subarrays?

Simple Approach: A simple approach is to run two nested loops and generate all subarrays of the array A [] and use one more loop to check if any of the subarray of A [] is equal to the array B []. Efficient Approach : An efficient approach is to use two pointers to traverse both the array simultaneously.


4 Answers

You can't do like that .instance you have to do some thing by your own .. first you have to do a foreach from your array that you want to search and run 'compareArray' function for each item of your array .

function compareArray( arrA, arrB ){

    //check if lengths are different
    if(arrA.length !== arrB.length) return false;


    for(var i=0;i<arrA.length;i++){
         if(arrA[i]!==arrB[i]) return false;
    }

    return true;

}
like image 45
Farhad Azarbarzinniaz Avatar answered Oct 23 '22 06:10

Farhad Azarbarzinniaz


Short and easy, stringify the array and compare as strings

function isArrayInArray(arr, item){
  var item_as_string = JSON.stringify(item);

  var contains = arr.some(function(ele){
    return JSON.stringify(ele) === item_as_string;
  });
  return contains;
}

var myArray = [
  [1, 0],
  [1, 1],
  [1, 3],
  [2, 4]
]
var item = [1, 0]

console.log(isArrayInArray(myArray, item));  // Print true if found

check some documentation here

like image 189
mohamed-ibrahim Avatar answered Oct 23 '22 08:10

mohamed-ibrahim


A nested array is essentially a 2D array, var x = [[1,2],[3,4]] would be a 2D array since I reference it with 2 index's, eg x[0][1] would be 2.

Onto your question you could use a plain loop to tell if they're included since this isn't supported for complex arrays:

var x = [[1,2],[3,4]];
var check = [1,2];
function isArrayInArray(source, search) {
    for (var i = 0, len = source.length; i < len; i++) {
        if (source[i][0] === search[0] && source[i][1] === search[1]) {
            return true;
        }
    }
    return false;
}
console.log(isArrayInArray(x, check)); // prints true

Update that accounts for any length array

function isArrayInArray(source, search) {
    var searchLen = search.length;
    for (var i = 0, len = source.length; i < len; i++) {
        // skip not same length
        if (source[i].length != searchLen) continue;
        // compare each element
        for (var j = 0; j < searchLen; j++) {
            // if a pair doesn't match skip forwards
            if (source[i][j] !== search[j]) {
                break;
            }
            return true;
        }
    }
    return false;
}
console.log(isArrayInArray([[1,2,3],[3,4,5]], [1,2,3])); // true
like image 31
dayvidwhy Avatar answered Oct 23 '22 08:10

dayvidwhy


Here is an ES6 solution:

myArray.some(
    r => r.length == itemTrue.length &&
         r.every((value, index) => itemTrue[index] == value)
);

Check the JSFiddle.

Take a look at arrow functions and the methods some and every of the Array object.

like image 39
Jefferson Lima Avatar answered Oct 23 '22 06:10

Jefferson Lima