Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check the array contains different values or same values in Jquery [duplicate]

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

like image 614
Shan Biswas Avatar asked Aug 03 '16 12:08

Shan Biswas


1 Answers

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()

like image 192
Rahul Tripathi Avatar answered Sep 28 '22 03:09

Rahul Tripathi