在对象数组中查找重复项,并选择其中一个比较它们的值

时间:2017-09-15 17:53:14

标签: javascript arrays lodash

var array1 = [
   {
    "_id" : ObjectId("5972dbe185c5a00906fbefa7"),
    "name" : "user1",
    "score" : 2.0
   },
   {
    "_id" : ObjectId("59994cf90b79bc4e8233ed1d"),
    "name" : "user2",
    "score" : 1.5
   },
   {
    "_id" : ObjectId("59880bbaa329573db1180acd"),
    "name" : "user3",
    "score" : 0.5
   },
   {
    "_id" : ObjectId("5972fa945ad42b149da788b2"),
    "name" : "user4",
    "score" : 3.0
   }
]

var array2 = [
   {
    "_id" : ObjectId("5972dbe185c5a00906fbefa7"),
    "name" : "user1",
    "score" : 1.5
   },
   {
    "_id" : ObjectId("599d2d8fe9feeb290c699535"),
    "name" : "user5",
    "score" : 1.5
   },
   {
    "_id" : ObjectId("59880bbaa329573db1180acd"),
    "name" : "user3",
    "score" : 3.0
   },
   {
    "_id" : ObjectId("8072fa945ad42b149da799cc"),
    "name" : "user8",
    "score" : 2.5
   }
]

array3 = _.unionWith(array1, array2, _.isEqual);

我有两个用户对象从两个不同的搜索查询返回,现在可能有重复的用户对象,我确实使用lodash的._unionWith函数删除了重复项,但是我没有选择并返回其中一个通过将另一个重复对象分数添加到要返回的对象的score属性来重复对象。 我怎样才能以更有效的方式做到这一点。感谢

2 个答案:

答案 0 :(得分:0)

使用循环你可以这样做:



var array1 = [
   {
    "_id" : "5972dbe185c5a00906fbefa7",
    "name" : "user1",
    "score" : 2.0
   },
   {
    "_id" : "59994cf90b79bc4e8233ed1d",
    "name" : "user2",
    "score" : 1.5
   },
   {
    "_id" : "59880bbaa329573db1180acd",
    "name" : "user3",
    "score" : 0.5
   },
   {
    "_id" : "5972fa945ad42b149da788b2",
    "name" : "user4",
    "score" : 3.0
   }
]

var array2 = [
   {
    "_id" : "5972dbe185c5a00906fbefa7",
    "name" : "user1",
    "score" : 1.5
   },
   {
    "_id" : "599d2d8fe9feeb290c699535",
    "name" : "user5",
    "score" : 1.5
   },
   {
    "_id" : "59880bbaa329573db1180acd",
    "name" : "user3",
    "score" : 3.0
   },
   {
    "_id" : "8072fa945ad42b149da799cc",
    "name" : "user8",
    "score" : 2.5
   }
]



array1.forEach((item)=>{
  array2.forEach((item2) => {
    if(item._id === item2._id){
      item2.score = item2.score +item.score;//add score from first array dup to second
    }
  
  });   
  });
console.log(array2)




现在array2具有array1

中所有重复项的分数

注意:我删除了ObjectId以使其在代码段中工作

答案 1 :(得分:0)

您可以将哈希表作为对象的引用并减少数组。



var array1 = [{ _id: "5972dbe185c5a00906fbefa7", name: "user1", score: 2.0 }, { _id: "59994cf90b79bc4e8233ed1d", name: "user2", score: 1.5 }, { _id: "59880bbaa329573db1180acd", name: "user3", score: 0.5 }, { _id: "5972fa945ad42b149da788b2", name: "user4", score: 3.0 }],
    array2 = [{ _id: "5972dbe185c5a00906fbefa7", name: "user1", score: 1.5 }, { _id: "599d2d8fe9feeb290c699535", name: "user5", score: 1.5 }, { _id: "59880bbaa329573db1180acd", name: "user3", score: 3.0 }, { _id: "8072fa945ad42b149da799cc", name: "user8", score: 2.5 }],
    hash = Object.create(null),
    result = [array1, array2].reduce(function (r, a) {
        a.forEach(function (o) {
            if (!hash[o._id]) {
                hash[o._id] = o;
                r.push(hash[o._id]);
                return;
            }
            hash[o._id].score += o.score;
        });
        return r;
    }, []);

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }