gpt4 book ai didi

javascript - 将二维数组中的 ID 与另一个二维数组中的 ID 进行比较

转载 作者:行者123 更新时间:2023-11-30 15:48:24 25 4
gpt4 key购买 nike

好的,这是按 ID 对 Javascript 对象数组进行分组的系列中的另一个,但这次,我们在数组对象 (item3) 中有一个 ID 数组,它将与另一个对象数组进行比较。

var existingArray = [
{
"item1": "Blah1",
"item2": "blah2",
"item3": ["0200","0300"],
"item4": "blah4",
"item5": "blah5"
},{
"item1": "Blah1",
"item2": "blah2",
"item3": ["0100","0300"],
"item4": "blah4",
"item5": "blah5"
},{
"item1": "Blah1",
"item2": "blah2",
"item3": ["0100"],
"item4": "blah4",
"item5": "blah5"
},{
"item1": "Blah1",
"item2": "blah2",
"item3": ["0300"],
"item4": "blah4",
"item5": "blah5"
},{
"item1": "Blah1",
"item2": "blah2",
"item3": ["0200", "0100"],
"item4": "blah4",
"item5": "blah5"
}
]

这是我们最喜欢的 DATA2 数组,如果“relatedId”与 EXISTINGARRAY 中的 item3 中的任何 ID 相同,它包含我们要提取的信息片段 (CandidateName)。

var data2 = [
{"CandidateName": "Mary", "relatedId": ["0100", "0200"]},
{ "CandidateName": "John", "relatedId": ["0200"]},
{ "CandidateName":"Peter", "relatedId": ["0300", "0100"]},
{ "CandidateName": "Paul", "relatedId": ["0300"]}
];

所以想法是,如果 data2[i].relatedId[j] 中的任何 ID === existingArray[k].item3[l],拉出“CandidateName”并将其添加到 EXISTINGARRAY 中,这样我们就结束了想出类似下面的东西。

existingArray = [
{
"item1": "Blah1",
"item2": "blah2",
"item3": ["0200","0300"],
"item4": "blah4",
"item5": "blah5",
"item6": ["Mary", "Jonh", "Peter", "Paul"]
},{
"item1": "Blah1",
"item2": "blah2",
"item3": ["0100","0300"],
"item4": "blah4",
"item5": "blah5",
"item6": ["Mary", "Peter", "Paul"]
},{
"item1": "Blah1",
"item2": "blah2",
"item3": ["0100"],
"item4": "blah4",
"item5": "blah5",
"item6": ["Mary", "Peter"]
},{
"item1": "Blah1",
"item2": "blah2",
"item3": ["0300"],
"item4": "blah4",
"item5": "blah5",
"item6": ["Peter", "Paul"]
},{
"item1": "Blah1",
"item2": "blah2",
"item3": ["0200", "0100"],
"item4": "blah4",
"item5": "blah5",
"item6": ["Mary", "John","Peter"]
}
]

最佳答案

这是一个 ES6 解决方案:

existingArray.forEach( function (obj) {
obj.item6 = [...new Set(obj.item3.reduce( (acc, id) => acc.concat(this.get(id)), [] ))]
}, data2.reduce (
(acc, obj) => obj.relatedId.reduce (
(acc, id) => acc.set(id, (acc.get(id) || []).concat(obj.CandidateName)), acc
), new Map()
));

var existingArray = [
{
"item1": "Blah1",
"item2": "blah2",
"item3": ["0200","0300"],
"item4": "blah4",
"item5": "blah5"
},{
"item1": "Blah1",
"item2": "blah2",
"item3": ["0100","0300"],
"item4": "blah4",
"item5": "blah5"
},{
"item1": "Blah1",
"item2": "blah2",
"item3": ["0100"],
"item4": "blah4",
"item5": "blah5"
},{
"item1": "Blah1",
"item2": "blah2",
"item3": ["0300"],
"item4": "blah4",
"item5": "blah5"
},{
"item1": "Blah1",
"item2": "blah2",
"item3": ["0200", "0100"],
"item4": "blah4",
"item5": "blah5"
}
]

var data2 = [
{"CandidateName": "Mary", "relatedId": ["0100", "0200"]},
{ "CandidateName": "John", "relatedId": ["0200"]},
{ "CandidateName":"Peter", "relatedId": ["0300", "0100"]},
{ "CandidateName": "Paul", "relatedId": ["0300"]}
];

existingArray.forEach( function (obj) {
obj.item6 = [...new Set(obj.item3.reduce( (acc, id) => acc.concat(this.get(id)), [] ))]
}, data2.reduce (
(acc, obj) => obj.relatedId.reduce (
(acc, id) => acc.set(id, (acc.get(id) || []).concat(obj.CandidateName)), acc
), new Map()
));

console.log(existingArray);

说明

代码真正从最后开始,创建了一个空的 Map。 :

new Map()

这成为在 acc 时累积数据的变量(名为 data2 )用 reduce 迭代:

data2.reduce

reduce嵌套操作以迭代每个 relatedId个别地。如果累加值 ( acc ) 尚未包含找到的 id,则创建一个新数组:

acc.get(id) || []

...否则使用找到的数组值。附加候选人姓名:

.concat(obj.CandidateName)

... 这被放回 accid :

acc.set(id, ...)

作为 set方法返回 acc本身,它与 reduce 配合得很好需要此返回值才能传递 acc再次调用回调的下一个调用。

外层的结果reduce电话是Map由所有 id 键入在 data2 中找到的值,每个值都是关联名称的数组。

此值然后作为第二个参数传递给 forEach调用,因此成为 this 的值.所以当你看到:

this.get(id)

它正在检索 id 的候选名称数组来自 Map如上所述。

forEach回调另一个reduce用于迭代 id item3 中的值:

obj.item3.reduce

这会累积到一个名称数组,然后传递给 Set构造函数:

new Set(...)

这样做是为了从名称数组中删除重复项。这Set使用扩展运算符立即再次转换为数组:

[...new Set()]

等等所有item6属性获得它们的值(value)。

关于javascript - 将二维数组中的 ID 与另一个二维数组中的 ID 进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39733354/

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