我有一个重复的对象数组。对象的根级别的数据相同,但是对象的嵌套数组不同。这是我在运行lodash uniqBy或任何函数以删除重复项之前需要进行的合并。
这是具有重复项的对象数组。
[
{
"id": "66E175A2-A29F-4F1A-AD81-2422B1EB00F6",
"name": "College Park / Brookhaven",
"mktId": 0,
"status": "Unknown",
"code": "197D6",
"ownershipType": null,
"series": [
{
"id": "80004F2E-E3C8-4B6A-BCCC-81259AEAF22D",
"name": "01",
"productType": "Detached",
"productClass": "Single Family",
"salesStartDate": null,
"modelOpenDate": null
}
]
},
{
"id": "E053E656-4D14-4F2A-AD70-A37F65195CD1",
"name": "College Park / Hampshire",
"mktId": 0,
"status": "Unknown",
"code": "316D6",
"ownershipType": null,
"series": [
{
"id": "46830FBD-CD68-4D4C-A095-FB9C3D93D01A",
"name": "02,03",
"productType": "Detached",
"productClass": "Single Family",
"salesStartDate": null,
"modelOpenDate": null
}
]
},
{
"id": "E053E656-4D14-4F2A-AD70-A37F65195CD1",
"name": "College Park / Hampshire",
"mktId": 0,
"status": "Unknown",
"code": "316D6",
"ownershipType": null,
"series": [
{
"id": "1BC31692-AAB8-4A00-9D8D-9B8CF7E426E0",
"name": "01",
"productType": "Detached",
"productClass": "Single Family",
"salesStartDate": null,
"modelOpenDate": null
}
]
},
{
"id": "34F7C7AF-3D1B-4EE7-8271-C99294169C01",
"name": "College Park / Hillsdale",
"mktId": 0,
"status": "Unknown",
"code": "295D6",
"ownershipType": null,
"series": [
{
"id": "807144A1-26ED-4657-9775-7DF7563107D3",
"name": "02",
"productType": "Detached",
"productClass": "Single Family",
"salesStartDate": null,
"modelOpenDate": null
}
]
}
]
在这个例子中,大学公园/汉普郡是重复的。我需要找到2个重复项并返回此预期结果。
[
{
"id": "66E175A2-A29F-4F1A-AD81-2422B1EB00F6",
"name": "College Park / Brookhaven",
"mktId": 0,
"status": "Unknown",
"code": "197D6",
"ownershipType": null,
"series": [
{
"id": "80004F2E-E3C8-4B6A-BCCC-81259AEAF22D",
"name": "01",
"productType": "Detached",
"productClass": "Single Family",
"salesStartDate": null,
"modelOpenDate": null
}
]
},
{
"id": "E053E656-4D14-4F2A-AD70-A37F65195CD1",
"name": "College Park / Hampshire",
"mktId": 0,
"status": "Unknown",
"code": "316D6",
"ownershipType": null,
"series": [
{
"id": "46830FBD-CD68-4D4C-A095-FB9C3D93D01A",
"name": "02,03",
"productType": "Detached",
"productClass": "Single Family",
"salesStartDate": null,
"modelOpenDate": null
},
{
"id": "1BC31692-AAB8-4A00-9D8D-9B8CF7E426E0",
"name": "01",
"productType": "Detached",
"productClass": "Single Family",
"salesStartDate": null,
"modelOpenDate": null
}
]
},
{
"id": "34F7C7AF-3D1B-4EE7-8271-C99294169C01",
"name": "College Park / Hillsdale",
"mktId": 0,
"status": "Unknown",
"code": "295D6",
"ownershipType": null,
"series": [
{
"id": "807144A1-26ED-4657-9775-7DF7563107D3",
"name": "02",
"productType": "Detached",
"productClass": "Single Family",
"salesStartDate": null,
"modelOpenDate": null
}
]
}
]
香草javascript或lodash都可以。
最佳答案
只需为id和结果数组设置一个哈希图:
const hash = {}, result = [];
然后遍历数组:
for(const el of array){
const { id, series } = el;
现在,我们可以检查ID是否已出现在哈希中,如果是,则只需添加序列:
if(hash[id]){
hash[id].series.push(...series);
} else {
如果id尚未出现,则需要将该对象添加到结果和哈希表中:
result.push(hash[id] = el)
}
}
就是这样:)