我有以下對象數組。我需要做的只是從所有連接器陣列中刪除匹配的鍵值對。從對象數組中刪除重複的元素 - es6?
[
{
"connector":[
{
"name":"CC1"
},
{
"name":"App1"
},
{
"name":"CC1"
},
{
"name":"App2"
},
{
"name":"CC1"
},
{
"name":"App3"
}
],
"connections":[
{
"source":"CC1",
"target":"App1"
},
{
"source":"CC1",
"target":"App2"
},
{
"source":"CC1",
"target":"App3"
}
]
},
{
"connector":[
{
"name":"CC1"
},
{
"name":"App1"
},
{
"name":"CC1"
},
{
"name":"App2"
},
{
"name":"CC1"
},
{
"name":"App3"
}
],
"connections":[
{
"source":"CC1",
"target":"App1"
},
{
"source":"CC1",
"target":"App2"
},
{
"source":"CC1",
"target":"App3"
}
]
},
{
"connector":[
{
"name":"CC1"
},
{
"name":"App1"
},
{
"name":"CC1"
},
{
"name":"App2"
},
{
"name":"CC1"
},
{
"name":"App3"
}
],
"connections":[
{
"source":"CC1",
"target":"App1"
},
{
"source":"CC1",
"target":"App2"
},
{
"source":"CC1",
"target":"App3"
}
]
},
{
"connector":[
{
"name":"CC2"
},
{
"name":"App2"
}
],
"connections":[
{
"source":"CC2",
"target":"App2"
}
]
}
]
我一直在使用過濾器,地圖和ES6可塗抹運營商的合併審理,但還沒有發現,將實現這一目標的最佳組合。 ,我想的輸出如下:
[
{
"connector":[
{
"name":"CC1"
},
{
"name":"App1"
},
{
"name":"App2"
},
{
"name":"App3"
}
],
"connections":[
{
"source":"CC1",
"target":"App1"
},
{
"source":"CC1",
"target":"App2"
},
{
"source":"CC1",
"target":"App3"
}
]
},
{
"connector":[
{
"name":"CC1"
},
{
"name":"App1"
},
{
"name":"App2"
},
{
"name":"App3"
}
],
"connections":[
{
"source":"CC1",
"target":"App1"
},
{
"source":"CC1",
"target":"App2"
},
{
"source":"CC1",
"target":"App3"
}
]
},
{
"connector":[
{
"name":"CC1"
},
{
"name":"App1"
},
{
"name":"App2"
},
{
"name":"App3"
}
],
"connections":[
{
"source":"CC1",
"target":"App1"
},
{
"source":"CC1",
"target":"App2"
},
{
"source":"CC1",
"target":"App3"
}
]
},
{
"connector":[
{
"name":"CC2"
},
{
"name":"App2"
}
],
"connections":[
{
"source":"CC2",
"target":"App2"
}
]
}
]
什麼是最優化的解決方案來實現這一目標?在此先感謝您的幫助..
只要你打算使用lodash。這是你如何做到的。 (i.connector = _.uniqBy(i.connector,'name'); return i; })' –
這是最好的,最理想的方式來做到這一點,將會在冗餘數據進入這個對象數組之前停止。 – PHPglue