简体   繁体   English

循环对象数组,比较键值

[英]Loop array of objects, compare key values

I have an array of objects, all with the same keys. 我有一个对象数组,所有对象都具有相同的键。 I want to check if 2 keys, in this case "a" & "b" from ALL OBJECTS, have the same value. 我想检查2个键(在这种情况下,“所有对象”中的“ a”和“ b”)是否具有相同的值。 Meaning a === a and b === b, across all objects. 跨所有对象的意义a === a和b === b。

var arr = [{a:"x", b:2, c:3, d:4},{a:1, b:2, c:3, d:4},{a:1, b:2, c:3, d:4},{a:1, b:2, c:3, d:4}];

if one of the key values(a or b) doesn't match the others, I want to return false. 如果其中一个键值(a或b)与其他键值不匹配,我想返回false。 This case should return false because "a" in arr[0] = "x", while all the others equal 1; 这种情况应该返回false,因为arr [0]中的“ a” =“ x”,而其他所有条件都等于1;否则,则返回false。

Here's my attempt. 这是我的尝试。 Obviously doesnt work but what I was attempting was to have 2 loops compare 2 objects at a time. 显然不起作用,但是我试图做的是让2个循环一次比较2个对象。 For example, the first loop will start at arr[0] and second loop will start at arr[1], compare then move onto comparing arr[3] with arr[4] and so on. 例如,第一个循环将从arr [0]开始,第二个循环将从arr [1]开始,进行比较然后移至将arr [3]与arr [4]进行比较,依此类推。 Though the length of the array wont always be even. 虽然数组的长度不会总是均匀的。

function compareKeyVals(arr){

  for(var i = 0; i<arr.length; i+=2){
    for(var j = 1; j<arr.length; j+=2){
     for(key in arr[i]){     
        for(keyz in arr[j]){      
          if(key === "a" && keyz === "a"){
            if(arr[i][key] != arr[j][keyz]){
              return false;
            }
          }  
        }
      }    
    }
  }
}

compareKeyVals(arr);

thanks. 谢谢。

Since they all have to be equal, you can just check the a and b properties of the first object and compare all others against that. 由于它们都必须相等,因此您只需检查第a对象的ab属性,然后将所有其他属性进行比较即可。 Like so: 像这样:

for(var i=1; i<arr.length; i++){
  if(arr[i].a !== arr[0].a || arr[i].b !== arr[0].b) {
    return false;
  }
}
return true;

Update: Changed to strict comparison, inspired by Marc's answer 更新:受马克的回答启发,改为严格比较

var flag = true;  
for(var i = 1; i<arr.length; i++){
if(arr[0].a != arr[i].a || arr[0].b != arr[i].b){
flag = false;
break;
 }
}
return flag;

You can really just detect a change from one iteration to the next, right? 您真的可以只检测一次迭代到下一次迭代的变化,对吗?

 var arrayLength = arr.length; //cache the array length
 if (arrayLength > 0) { //only proceed if there are some elements
   var firstA = arr[0].a; //set the baseline that all others must match
   var firstB = arr[0].b;
   for(var i = 1; i<arrayLength ; i++) {
       //if the new one doesn't match the first one, break out
       if (firstA !== arr[i].a || firstB !== arr[i].b) {
           return false;
       }
   }
   return true; //return true if we complete the loop with all matches
 } else {
   return false; //assumes you want to return false if no elements in array
 }

This will break you out of the loop immediately when it finds a mismatch, so you're not wasting iterations. 一旦发现不匹配,这将使您立即退出循环,因此您不会浪费迭代。

(added a check for empty array per comment) (添加了对每个评论的空数组的检查)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM