如何根据数组的值过滤数组数组

时间:2017-07-17 21:08:35

标签: javascript arrays

我有两个数组playerMovesmovesList。像这样。

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

我需要过滤movesList数组,以使playerMoves数组的值不应出现在movesList的每个数组中。

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

我的尝试

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

1 个答案:

答案 0 :(得分:3)

您可以使用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));