我正在尝试过滤父数组的子数组,但返回的结果为空。我正在尝试找到与 color_family 匹配的内容。
我的数组看起来像:
const arr =[
{
"id": 123,
"acf": {
"product_colors": [
{
"color_family": "grey"
},
{
"color_family": "taupe"
}
]
}
},
{
"id": 456,
"acf": {
"product_colors": [
{
"color_family": "red"
},
{
"color_family": "taupe"
}
]
}
}
]
我过滤的是
const findColors = ["grey", "taupe"]
我试过没有运气的是
const res = arr.filter(
x => x.acf.product_colors.find(
color_family => findColors.includes(color_family)
)
)
当它应该返回 2 个结果时却没有返回任何结果。有人可以指出我正确的方向吗?
最佳答案
除了拼写错误之外,find
参数的参数是一个具有color_family
的对象:
const res = arr.filter(x => x.acf.product_colors.find(col => {
return findColors.includes(col.color_family);
}))
关于javascript - 过滤数组的子数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55072680/