我有一个对象数组,每个对象都由一个嵌套数组组成。我需要根据嵌套数组中的ID过滤主数组。具有重复ID的对象应从结果中删除。
对象数组具有嵌套数组,其中有2个参数:"AuN": "sanjay mittal"
和对应的"AuId"
。我需要根据"AuId"
附带的"AuN": "sanjay mittal"
的值来使主数组对象唯一。
数据:
[
{
"CC": 623,
"AA": [
{
"AuN": "tayfun e tezduyar",
"AuId": 166714465
},
{
"AuN": "sanjay mittal",
"AuId": 2141608163
}
],
"PK": 2009478656
},
{
"CC": 488,
"AA": [
{
"AuN": "tayfun e tezduyar",
"AuId": 166714465
},
{
"AuN": "marek behr",
"AuId": 2117578800
},
{
"AuN": "sanjay mittal",
"AuId": 2141608163 // duplicate data. Need to remove the main object
}
],
"PK": 1973948837
},
{
"CC": 118,
"AA": [
{
"AuN": "rajendra k premchand",
"AuId": 1987062001
},
{
"AuN": "kamal sharma",
"AuId": 2426730634
},
{
"AuN": "sanjay mittal",
"AuId": 2583035735
},
{
"AuN": "inder s anand",
"AuId": 1907699963
}
],
"PK": 2116095873
},
{
"CC": 401,
"AA": [
{
"AuN": "sanjay mittal",
"AuId": 2123420744
},
{
"AuN": "brian falkenhainer",
"AuId": 68193438
}
],
"PK": 153230822
},
{
"CC": 86,
"AA": [
{
"AuN": "sanjay mittal",
"AuId": 2123420744
},
{
"AuN": "shulian shang",
"AuId": 2723036630
},
{
"AuN": "todd j mortier",
"AuId": 2063047595
},
{
"AuN": "cyril j schweich",
"AuId": 2132794328
}
],
"PK": 2021819698
}
]
预期结果:
我的预期结果基于名称"AuN": "sanjay mittal"
和唯一的"AuId"
删除了一些主要对象:
var expectedArray = [
{
"CC": 623,
"AA": [
{
"AuN": "tayfun e tezduyar",
"AuId": 166714465
},
{
"AuN": "sanjay mittal",
"AuId": 2141608163
}
],
"PK": 2009478656
},
{
"CC": 118,
"AA": [
{
"AuN": "rajendra k premchand",
"AuId": 1987062001
},
{
"AuN": "kamal sharma",
"AuId": 2426730634
},
{
"AuN": "sanjay mittal",
"AuId": 2583035735
}
{
"AuN": "inder s anand",
"AuId": 1907699963
}
],
"PK": 2116095873
},
{
"CC": 401,
"AA": [
{
"AuN": "sanjay mittal",
"AuId": 2123420744
},
{
"AuN": "brian falkenhainer",
"AuId": 68193438
}
],
"PK": 153230822
},
]
我正在尝试找到一种有效的方法。任何见解和方法都可以帮助我实现这一目标。
答案 0 :(得分:2)
您可以为此使用Map
。通过相关的AuId对其进行键控,并为其存储相应的顶级对象。如果以相反的顺序执行此操作,则相同AuId的首次出现将在地图中保留。
然后只需再次从地图上获取这些对象,就可以完成:
const array = [{"CC":623,"AA":[{"AuN":"tayfun e tezduyar","AuId":166714465},{"AuN":"sanjay mittal","AuId":2141608163}],"PK":2009478656},{"CC":488,"AA":[{"AuN":"tayfun e tezduyar","AuId":166714465},{"AuN":"marek behr","AuId":2117578800},{"AuN":"sanjay mittal","AuId":2141608163}],"PK":1973948837},{"CC":118,"AA":[{"AuN":"rajendra k premchand","AuId":1987062001},{"AuN":"kamal sharma","AuId":2426730634},{"AuN":"sanjay mittal","AuId":2583035735},{"AuN":"inder s anand","AuId":1907699963}],"PK":2116095873},{"CC":401,"AA":[{"AuN":"sanjay mittal","AuId":2123420744},{"AuN":"brian falkenhainer","AuId":68193438}],"PK":153230822},{"CC":86,"AA":[{"AuN":"sanjay mittal","AuId":2123420744},{"AuN":"shulian shang","AuId":2723036630},{"AuN":"todd j mortier","AuId":2063047595},{"AuN":"cyril j schweich","AuId":2132794328}],"PK":2021819698}];
const map = new Map(array.reverse().map(obj =>
[obj.AA.find(({AuN}) => AuN === "sanjay mittal").AuId, obj]
));
const filtered = Array.from(map.values()).reverse();
console.log(filtered);
此代码假定每个对象都具有这样的“ sanjay mittal”条目。如果不能保证,那么您需要添加一些测试并决定如何处理此类“孤立”对象。
在您提到的评论中,您会收到有关 AuId 属性的错误。这意味着以上最后一段中的假设是不正确的。因此,这是解决这种情况的方法:
const array = [{"CC":623,"AA":[{"AuN":"tayfun e tezduyar","AuId":166714465},{"AuN":"sanjay mittal","AuId":2141608163}],"PK":2009478656},{"CC":488,"AA":[{"AuN":"tayfun e tezduyar","AuId":166714465},{"AuN":"marek behr","AuId":2117578800},{"AuN":"sanjay mittal","AuId":2141608163}],"PK":1973948837},{"CC":118,"AA":[{"AuN":"rajendra k premchand","AuId":1987062001},{"AuN":"kamal sharma","AuId":2426730634},{"AuN":"sanjay mittal","AuId":2583035735},{"AuN":"inder s anand","AuId":1907699963}],"PK":2116095873},{"CC":401,"AA":[{"AuN":"s*a*n*jay mittal","AuId":2123420744},{"AuN":"brian falkenhainer","AuId":68193438}],"PK":153230822},{"CC":86,"AA":[{"AuN":"sanjay mittal","AuId":2123420744},{"AuN":"shulian shang","AuId":2723036630},{"AuN":"todd j mortier","AuId":2063047595},{"AuN":"cyril j schweich","AuId":2132794328}],"PK":2021819698}];
const map = new Map(array.reverse().map((obj, i) => {
const found = obj.AA.find(({AuN}) => AuN === "sanjay mittal");
if (found) return [found, obj];
return [-i, [...obj.AA, { "AuN":"sanjay mittal","AuId": "missing" }]];
}));
const filtered = Array.from(map.values()).reverse();
console.log(filtered);
这将检测到缺少“ sanjay mittal”条目,然后认为该对象不是重复项,然后将条目“ sanjay mittal” /“ missing”添加到过滤数据中。