如何迭代一系列数组以过滤或拒绝重复条目?
我想迭代这个二维阵列。带有多个对象的数组在同一个月(在1月以下的示例中)具有多个条目。我想过滤(拒绝)重复条目,并想返回更改的数组。
我在这里尝试了不同的解决方案,但到目前为止失败了。我将感谢任何投入!
[[
{ "id":"9","bookingReason":"Netto Neu Eigen","bookingDate":"2021-01-22","bookingType":"Gutschrift","bookingPoints":"100" },
{ "id":"10","bookingReason":"Netto Neu Eigen","bookingDate":"2021-01-23","bookingType":"Gutschrift","bookingPoints":"100" }
], [
{ "id":"9","bookingReason":"Netto Neu Eigen","bookingDate":"2021-01-22","bookingType":"Gutschrift","bookingPoints":"100" },
{ "id":"10","bookingReason":"Netto Neu Eigen","bookingDate":"2021-01-23","bookingType":"Gutschrift","bookingPoints":"100" }
], [
{ "id":"29","bookingReason":"Netto Neu Eigen","bookingDate":"2022-01-22","bookingType":"Gutschrift","bookingPoints":"100" },
{ "id":"30","bookingReason":"Netto Neu Eigen","bookingDate":"2022-01-23","bookingType":"Gutschrift","bookingPoints":"100" }
], [
{ "id":"29","bookingReason":"Netto Neu Eigen","bookingDate":"2022-01-22","bookingType":"Gutschrift","bookingPoints":"100" },
{ "id":"30","bookingReason":"Netto Neu Eigen","bookingDate":"2022-01-23","bookingType":"Gutschrift","bookingPoints":"100" }
]]
对Traktor的编辑答案: 我想最低限度是检查每个子阵列是否唯一。 因此,如果过滤器第二次找到相同的ID,则应删除整个重复数组。
编辑对彼得·塞利格的答案: 结果应保持二维结构。
[
[{
"id": "1",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2021-09-22",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}], [{
"id": "2",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2021-08-22",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}], [{
"id": "3",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2021-07-22",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}], [{
"id": "4",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2021-06-22",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}], [{
"id": "5",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2021-05-22",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}], [{
"id": "6",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2021-04-22",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}], [{
"id": "7",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2021-03-22",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}, {
"id": "12",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2021-03-24",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}], [{
"id": "8",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2021-02-22",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}, {
"id": "11",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2021-02-23",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}], [{
"id": "9",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2021-01-22",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}, {
"id": "10",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2021-01-23",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}], [{
"id": "9",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2021-01-22",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}, {
"id": "10",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2021-01-23",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}], [{
"id": "8",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2021-02-22",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}, {
"id": "11",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2021-02-23",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}], [{
"id": "7",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2021-03-22",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}, {
"id": "12",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2021-03-24",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}], [{
"id": "21",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2022-09-22",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}], [{
"id": "22",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2022-08-22",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}], [{
"id": "23",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2022-07-22",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}], [{
"id": "24",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2022-06-22",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}], [{
"id": "25",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2022-05-22",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}], [{
"id": "26",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2022-04-22",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}], [{
"id": "27",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2022-03-22",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}, {
"id": "32",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2022-03-24",
"bookingType": "Gutschrift",
"bookingPoints": "200"
}], [{
"id": "28",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2022-02-22",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}, {
"id": "31",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2022-02-23",
"bookingType": "Gutschrift",
"bookingPoints": "400"
}], [{
"id": "29",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2022-01-22",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}, {
"id": "30",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2022-01-23",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}], [{
"id": "29",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2022-01-22",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}, {
"id": "30",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2022-01-23",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}], [{
"id": "28",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2022-02-22",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}, {
"id": "31",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2022-02-23",
"bookingType": "Gutschrift",
"bookingPoints": "400"
}], [{
"id": "27",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2022-03-22",
"bookingType": "Gutschrift",
"bookingPoints": "100"
}, {
"id": "32",
"bookingReason": "Netto Neu Eigen",
"bookingDate": "2022-03-24",
"bookingType": "Gutschrift",
"bookingPoints": "200"
}]
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
OP实际上对...
...是,OP想要直接突变提供的数据结构或a 结构化的克隆。这变得更加清楚...
因此,必须提出一个递归工作的 拒绝 in >方法。
提供的实现使用自定义密钥(属性名称)特定值的查找。如果值尚不存在,则进行迭代进行进行,但值将分配给查找。任何具有相同功能(已经分配过的)的数组项目,值获取
剪接
来自数组的ED(这是一个突变任务),并将其推入本地拒绝
阵列。该函数的返回值是一个具有两个数组的对象...突变
,引用了传递和处理/突变的数据结构和被拒绝的
,它是项目的平坦列表从提供的数据结构中拒绝了。What the OP actually means with ...
... is that the OP wants to mutate either the provided data structure directly or maybe a structured clone of it. This becomes more clear with ...
Thus one has to come up with a recursively working rejecting approach.
The provided implementation uses a lookup for a custom key (property name) specific value. In case the value does not yet exist, the iteration proceeds but the value gets assigned to the lookup. Any array item which features the same, already assigned, value gets
splice
ed from the array (which is a mutation task) and pushed into the localrejected
array. The function's return value is an object which features two arrays ...mutated
with a reference to the passed and processed/mutated data structure andrejected
which is a flat list of items that were rejected from the provided data structure.我不确定我是否正确阅读了这个问题,但是如果您需要做的就是将这个破烂的二维阵列和从任何嵌套阵列中的重复条目过滤出来,那么我认为这很简单:
我们写一些我通常试图避免的东西:一个状态函数在迄今为止看到的一组可变的
id
s上关闭。此功能用作过滤器
在嵌套数组上调用的回调。我倾向于避免使用状态的功能,但是替代方案似乎是复杂的嵌套褶皱,这效果很好。我假设我们不希望删除任何基本重复项后留下的空心小组。如果该假设是错误的,我们可以简单地删除最终
过滤器
调用。该版本在句法上密集。以下变体可能对某些人更具吸引力:
他们做同样的事情。
I'm not sure if I'm reading the question correctly, but if all you need to do is take this ragged 2-d array and filter out duplicate entries from any of those nested arrays, then I think it's pretty simple:
We write something I usually try to avoid: a stateful function closing over a mutable Set of the
id
s we've seen so far. This function is used as the callback forfilter
calls on the nested arrays. I tend to avoid stateful functions, but the alternative seemed to complex be nested folds, and this works well.I made the assumption that we don't want empty groups left after removing any basic duplicates. If that assumption is wrong, we can simply remove the final
filter
call.That version is syntactically dense. The following variant might be more appealing to some folks:
They do the same thing.