According to documentation,
The $.inArray() method is similar to JavaScript's native .indexOf() method in that it returns -1 when it doesn't find a match. If the first element within the array matches value, $.inArray() returns 0.
That's exactly how it works:
inArray: function( elem, arr, i ) {
return arr == null ? -1 : indexOf.call( arr, elem, i );
}
It finds the occurcence of your elem in arr. It does not find the occurence of obejcts of an array in another array.
If you want to check that all items in products exist in compareProducts, you may iterate over the array or use Array.prototype.every:
var products = [];
products.push("ABC", "XYZ");
var compareProducts = ["ABC","XYZ"];
if (products.every(function(x) { return $.inArray(x, compareProducts) > -1; }))
{
alert("found");
} else {
alert("not found");
}
$.inArray(needle, haystack);You are providing both paramerters as array.