从第二个数组比较后如何从数组中删除项目(对象)?
How to Remove items (Object) from an Array after comparing from 2nd array?
我如何删除第一个数组中的 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;
)
注意这个recordtype的比较是区分大小写的;我假设你的例子不是故意忽略大小写。
如果您要处理大型数据集,我建议使用哈希表将其从 O(n^2)
过程减少到 O(n)
过程。
我如何删除第一个数组中的 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;
)
注意这个recordtype的比较是区分大小写的;我假设你的例子不是故意忽略大小写。
如果您要处理大型数据集,我建议使用哈希表将其从 O(n^2)
过程减少到 O(n)
过程。