[数组内部数组的过滤器

问题描述 投票:-1回答:1

如果数组也包含某个数组,我想过滤一个数组。

这里是数据:

let data = [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6]]
let to_filter = [2, 5]

我要过滤数据数组,如果其中的数组元素包括to_filter数组中的元素。所以预期的输出是

[[1, 2], [2, 3], [4, 5], [5, 6]]

这可能吗?有任何想法吗?谢谢。

javascript arrays
1个回答
1
投票

您可以使用someincludes

这里我正在检查to_filter是否包含子数组的任何元素,如果没有过滤掉该子数组的话

const data = [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6]]
const to_filter = [2, 5]

const output = data.filter(
    subArray => subArray.some(x => to_filter.includes(x))
)

console.log(output)
© www.soinside.com 2019 - 2024. All rights reserved.