-3
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的重複項._union具有函數,但是我未能通過向要返回的對象的score屬性添加另一個重複對象分數來選擇並返回其中一個重複對象。我如何以更有效的方式來做到這一點。感謝查找重複的對象數組,並選擇其中一個比較它們的值
發佈您的代碼將是一個良好的開端。 – TheCog
歡迎來到SO。請訪問[幫助],看看有什麼和如何問。建議:投入努力和代碼 – mplungjan
哦對不起!因爲這是我的第一個問題,我認爲我的問題描述足以被理解。 –