简体   繁体   English

如何基于数组的值过滤数组的数组

[英]How to filter an array of arrays based on values of an array

I have two arrays playerMoves and movesList . 我有两个数组playerMovesmovesList Like this. 像这样。

var playerMoves= [4, 6];
var movesList= [[0,1,2],[0,3,6]];

I need to filter the movesList array such that values of playerMoves array should not be present in each array of movesList . 我需要过滤movesList阵列,使得值playerMoves阵列不应存在的每个阵列中movesList

console.log(move);
// should return [0,1,2]

My attempt 我的尝试

var playerMoves= [4, 6];
var movesList= [[0,1,2],[0,3,6]];
var move =  movesList.filter(v => v.filter(c => {
   return playerMoves.indexOf(c) === -1;
}));
console.log(move);

You can use mix of Array#filter , Array#every and Array#includes . 您可以混合使用Array#filterArray#everyArray#includes

 let playerMoves = [4, 6]; let movesList = [ [0, 1, 2], [0, 3, 6], [5, 7, 9], ]; let res = movesList.filter(v => v.every(c => !playerMoves.includes(c))); console.log(JSON.stringify(res)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM