Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if one array is contained in another array

I have

['a', 'b', 'c']

I want to know if this array is contained in this array:

['a', 'b', 'c', 'd']

I know I can do 2 for loops and check item per item, but is there a oneliner for it?

like image 483
doniyor Avatar asked Aug 05 '15 14:08

doniyor


1 Answers

You can do this using Array.prototype.some. This will run the provided function against all the items in an array, and return true if the function returns true for any of them. The following will return true if any items from array are not contained in otherArray, which you can use to determine if one array is fully contained in the other:

return !array.some(function(item) {
   return otherArray.indexOf(item) === -1;
});

However, this is not the most elegant solution. The logic can be summed up as:

not any items from array not in other array

Which has far too many negatives. We can instead use Array.prototype.every, which is very similar except it returns true only if all items in an array return true for the provided function. The below is equivalent to what we had before:

return array.every(function(item) {
   return otherArray.indexOf(item) !== -1;
});

Except that can be summed up as:

all items in array in other array

Finally, we can implement this as an additional prototype function. Note that the second parameter for every is optional, and sets what this refers to in the function when provided. If we did not pass it in, we would not be able to refer to the this from the outside scope.

Array.prototype.contains = function(array) {
    return array.every(function(item) {
        return this.indexOf(item) !== -1;
    }, this);
}

This can now be used as a one liner in our code:

['a', 'b', 'c'].contains(['a', 'b']) // returns true

If you are able to use ECMAScipt 6, you can use arrow functions to make this a true one-liner.

return array.every(item => otherArray.indexOf(item) !== -1);
like image 73
James Brierley Avatar answered Sep 26 '22 11:09

James Brierley