我在下面的代码中有两个数组 - 其中 matches
是主要的,另一个 played
的目的是在主数组中过滤掉元素:
var matches = [[1,4],[3,1],[5,2],[3,4],[4,5],[2,1]];
var played = [2,5];
我需要根据已播放的数组过滤掉匹配中的元素,这意味着如果有 2 或 5 个元素,则将其完全删除。并且播放的数组可以是任意长度,最小值为1。
预期输出应该是
[[1,4],[3,1],[3,4]];
所以我尝试了这段代码,但它没有产生我想要的结果。
var result = matches.map(x => x.filter(e => played.indexOf(e) < 0))
那么无论如何要实现这个目标吗?
最佳答案
您可以检查some
并排除不需要的数组。
var matches = [[1, 4], [3, 1], [5, 2], [3, 4], [4, 5], [2, 1]],
played = [2, 5],
result = matches.filter(a => !a.some(v => played.includes(v)));
console.log(result);
关于javascript - 根据另一个数组元素过滤多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60939022/