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?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With