Is there an easy way of knowing if and array contains all elements of another array?
Example:
var myarray = [1,2,3];
var searchinarray = [1,2,3,4,5];
searchinarray contains all elements of myarray, so this should return true.
Regards
Is there an easy way of knowing if and array contains all elements of another array? Example:
searchinarray contains all elements of myarray, so this should return true. Regards |
|||
|
A contains function should only visit members of the array to be contained that exist, e.g.
should return true, however a simple looping solution will return false as
so that:
You can add a contains method to all instances of Array if you wish:
You may need a polyfill for browsers that don't have .every (IE 8?). |
|||
|
Here is an implementation of that function:
This shows that it does what you describe:
|
|||||
|
|
|||
|