Arrays 在从第二个数组进行比较后,如何从数组中删除项(对象)?
如何删除第一个数组中与第二个数组的Arrays 在从第二个数组进行比较后,如何从数组中删除项(对象)?,arrays,json,object,suitescript2.0,Arrays,Json,Object,Suitescript2.0,如何删除第一个数组中与第二个数组的recordType和id匹配的recordType和id对象。 请帮帮我,我困得很厉害 我的第一个阵列: [ { "recordType": "cashsale", "id": "208336", "values": { "entity": [ { "value": "141149", "text": "7457 abc company" }
recordType
和id
匹配的recordType
和id
对象。
请帮帮我,我困得很厉害
我的第一个阵列:
[
{
"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)
过程。简单!而且是最好的。谢谢你,理查德!:)对不起,我错给它打分了:(别担心,没关系。