【发布时间】:2022-11-19 23:57:19
【问题描述】:
当我想通过数组过滤数组内的数组时,我遇到了一个问题。请看例子-
const array1 = [
{
name: "this is name1",
products: [
{ id: "4" },
{ id: "2" },
]
},
{
name: "this is name2",
products: [
{ id: "2" },
{ id: "1" }
]
}
]
const array2 = [
{ id: "1", refund: true },
{ id: "2", refund: false },
{ id: "3", refund: true },
{ id: "4", refund: false}
]
在这里,我必须过滤array1 products 字段。在 array1 products 中提交了一个带有 id 的数组。我必须通过按 id 从 array2 中搜索相同的对象来过滤此产品字段,然后在退款为真时进行过滤。
从这个例子我需要结果 -
const array1 = [
{
name: "this is name2",
products: [
{ id: "1" }
]
}
]
结果我们只能看到这个数组中的一个对象。因为从array1开始,在对象的product filed中有两个id 4和2。从 array2 我们可以看到 id 4 和 2 的退款 false。这就是为什么 array1 删除第一个对象。
在第二个对象中,我们可以看到产品字段包含两个 id 2 和 1。从array2我们可以看到退款是false id 2 但退款是true id 1。因此对于 id 1 refund 是 true 这就是它留在产品数组中的原因。
请帮我。我希望我能解决我的问题。
【问题讨论】:
-
问题是什么?
标签: javascript