gpt4 book ai didi

javascript - javascript中基于2个键值对的排序

转载 作者:行者123 更新时间:2023-12-01 01:03:40 29 4
gpt4 key购买 nike

我有一个对象数组,每个对象都包含一个嵌套数组。我需要根据嵌套数组中的 ID 过滤主数组。应从结果中删除具有重复 ID 的对象。

对象数组具有嵌套数组,其中有 2 个参数:"AuN": "sanjay mittal" 和相应的 "AuId"。我需要根据 "AuN": "sanjay mittal"

附带的 "AuId" 值使我的主数组对象唯一

数据:

[
{
"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
},
]

我正在尝试找到一种有效的方法来做到这一点。任何见解和方法都可以帮助我实现这一目标。

最佳答案

您可以为此使用Map。通过相关的AuId来键入它,并为其存储相应的顶级对象。如果您以相反的顺序执行此操作,则第一次出现的相同 AuId 将保留在 map 中。

然后只需从 map 上再次取出这些对象即可完成:

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”添加到过滤后的数据中。

关于javascript - javascript中基于2个键值对的排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55823130/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com