如何删除第一阵列中具有recordType
和ids
与第二阵列的recordType
和ids
匹配的对象。请帮助我,我被卡住了。
我的第一个数组:
[
{
"recordType": "cashsale",
"id": "208336",
"values": {
"entity": [
{
"value": "141149",
"text": "7457 abc company"
}
]
}
},
{
"recordType": "cashsale",
"id": "582344",
"values": {
"entity": [
{
"value": "312",
"text": "2001 efg Group"
}
]
}
},
{
"recordType": "cashsale",
"id": "532294",
"values": {
"entity": [
{
"value": "133319",
"text": "1356 xyz group -"
}
]
}
},
{
"recordType": "cashsale",
"id": "139204",
"values": {
"entity": [
{
"value": "133319",
"text": "2357 xyz group -"
}
]
}
},
{
"recordType": "cashsale",
"id": "900994",
"values": {
"entity": [
{
"value": "133319",
"text": "3357 ssf group -"
}
]
}
}
]
我的第二个数组:
[
{
"id": "17271",
"recordType": "CashSale"
},
{
"id": "18469",
"recordType": "CashSale"
},
{
"id": "208336",
"recordType": "CashSale"
},
{
"id": "35406",
"recordType": "CashSale"
},
{
"id": "900994",
"recordType": "CashSale"
},
{
"id": "208336",
"recordType": "CashSale"
}
]
期望的输出:没有删除第一项和第二项的我的第一数组。
[
{
"recordType": "cashsale",
"id": "582344",
"values": {
"entity": [
{
"value": "312",
"text": "2001 efg Group"
}
]
}
},
{
"recordType": "cashsale",
"id": "532294",
"values": {
"entity": [
{
"value": "133319",
"text": "1356 xyz group -"
}
]
}
},
{
"recordType": "cashsale",
"id": "139204",
"values": {
"entity": [
{
"value": "133319",
"text": "2357 xyz group -"
}
]
}
}
]
天真的方法只是做一个嵌套循环;除非您要处理很多条目,否则性能是可以接受的。您可以使用filter
简化代码。就像这样:
firstArray = firstArray.filter(x => {
for (let y of secondArray) {
if (x.id === y.id && x.recordType === y.recordType) {
return false;
}
}
return true;
)
请注意,此记录类型的比较区分大小写;我假设您的示例不是故意忽略大小写。
如果您要处理大型数据集,建议使用哈希表将其从O(n^2)
进程减少为O(n)
一个。