简体   繁体   中英

How to filter an array of arrays based on values of an array

I have two arrays playerMoves and movesList . 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 .

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 .

 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)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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