我需要找到所有值都相等的数组。最快的方法是什么?我应该循环遍历它,然后比较值吗?

['a', 'a', 'a', 'a'] // true
['a', 'a', 'b', 'a'] // false

当前回答

公认的答案很好,但我想补充一点。对我来说,使用===行不通,因为我在比较对象数组的数组,但是在我的应用程序中,我一直在使用我强烈推荐的fast-deep-equal包。这样,我的代码看起来就像这样:

let areAllEqual = arrs.every((val, i, arr) => equal(val, arr[0]) );

我的数据是这样的:

[  
  [
    {
      "ID": 28,
      "AuthorID": 121,
      "VisitTypeID": 2
    },
    {
      "ID": 115,
      "AuthorID": 121,
      "VisitTypeID": 1
    },
    {
      "ID": 121,
      "AuthorID": 121,
      "VisitTypeID": 1
    }
  ],
  [
    {
      "ID": 121,
      "AuthorID": 121,
      "VisitTypeID": 1
    }
  ],
  [
    {
      "ID": 5,
      "AuthorID": 121,
      "VisitTypeID": 1
    },
    {
      "ID": 121,
      "AuthorID": 121,
      "VisitTypeID": 1
    }
  ]
]

其他回答

在PHP中,有一个非常简单的解决方案,一行方法:

(count(array_count_values($array)) == 1)

例如:

$arr1 = ['a', 'a', 'a', 'a'];
$arr2 = ['a', 'a', 'b', 'a'];


print (count(array_count_values($arr1)) == 1 ? "identical" : "not identical"); // identical
print (count(array_count_values($arr2)) == 1 ? "identical" : "not identical"); // not identical

这是所有。

公认的答案很好,但我想补充一点。对我来说,使用===行不通,因为我在比较对象数组的数组,但是在我的应用程序中,我一直在使用我强烈推荐的fast-deep-equal包。这样,我的代码看起来就像这样:

let areAllEqual = arrs.every((val, i, arr) => equal(val, arr[0]) );

我的数据是这样的:

[  
  [
    {
      "ID": 28,
      "AuthorID": 121,
      "VisitTypeID": 2
    },
    {
      "ID": 115,
      "AuthorID": 121,
      "VisitTypeID": 1
    },
    {
      "ID": 121,
      "AuthorID": 121,
      "VisitTypeID": 1
    }
  ],
  [
    {
      "ID": 121,
      "AuthorID": 121,
      "VisitTypeID": 1
    }
  ],
  [
    {
      "ID": 5,
      "AuthorID": 121,
      "VisitTypeID": 1
    },
    {
      "ID": 121,
      "AuthorID": 121,
      "VisitTypeID": 1
    }
  ]
]
**// Logical Solution:- Declare global array and one variable(To check the condition) whether all element of an array contains same value or not.**

    var arr =[];
    var isMatching = false;

    for(var i=0;i<arr.length;i++){
            if(String(arr[i]).toLowerCase()== "Your string to check"){
                isMatching=true;
                // Array has same value in all index of an array
            }
            else{
                isMatching=false;
                // Array Doesn't has same value in all index of an array
                break;
            }
        }
    // **Check isMatching variable is true or false**

        if(isMatching){ // True
            //If Array has same value in all index, then this block will get executed
        }
        else{ //False
            //If Array doesn't has same value in all index, then this block will get executed
        }

each()函数检查数组中的所有元素

    const checkArr = a => a.every( val => val === a[0] )
    checkArr(['a','a','a'])  // true 
     

更新2022版本:使用Set()

     let a = ['a', 'a', 'b', 'a'];
     let b = ['a', 'a', 'a', 'a'];
     const check = (list) => {
        const setItem = new Set(list);
        return setItem.size <= 1;
     }

     const checkShort = (list) => (new Set(list)).size <= 1        
     
      check(a); // false;
      check(b); // true;
      checkShort(a); // false
      checkShort(b); // true

更新新的解决方案:检查索引

 let a = ['a', 'a', 'b', 'a'];
 let b = ['a', 'a', 'a', 'a'];
 let check = (list) => list.every(item => list.indexOf(item) === 0);
 check(a); // false;
 check(b); // true;
   

在ES6更新: 使用列表。每一种方法都是最快的:

 let a = ['a', 'a', 'b', 'a'];
 let check = (list) => list.every(item => item === list[0]);
   

旧版本:

      var listTrue = ['a', 'a', 'a', 'a'];
      var listFalse = ['a', 'a', 'a', 'ab'];

      function areWeTheSame(list) { 
         var sample = list[0];
         return (list.every((item) => item === sample));
      }