An array can contains multiple values. I want to check whether all the values are same or different.
Example
var a = [2, 4, 7, 2, 8]; // all values are not same
var b = [2, 2, 2, 2, 2]; // all values are same
How can I check it in jquery
You can try like this:
var a = [2, 4, 7, 2, 8];
var b = [2, 2, 2, 2, 2];
function myFunc(arr){
var x= arr[0];
return arr.every(function(item){
return item=== x;
});
}
alert(myFunc(a));
alert(myFunc(b));
See the MDN for Array.prototype.every()
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