So, I\'m using Jquery and have two arrays both with multiple values and I want to check whether all the values in the first array exist in the second.
If you need a little bit more visibility on which items are in the array you can use this one :
var tools = {
elem : {},
arrayContains : function(needles, arrhaystack) {
if (this.typeOf(needles) === 'array') {
needle.reduce(function(result,item,$i,array){ // You can use any other way right there.
var present = (arrhaystack.indexOf(item) > -1);
Object.defineProperty(tools.elem, item, {
value : present,
writable : true
});
},{})
return this.elem;
}
},
typeOf : function(obj) {
return {}.toString.call(obj).split(' ')[1].slice(0, -1).toLowerCase();
}
}
Use it with simply var check = tools.arrayContains([10,'foo'], [1,'foo','bar'])
Then you get the result like
10 : false
foo : true
Then if you need to get only one result if one of them is true you can :
arr = Object.values(check);
(arr.indexOf('true')) ? instru1 : instru2 ;
I don't think that's the better way but it's working & easily adaptable.
Considering this example I advise you to make an Object.create(tools)
before use it in your way.