[英]How to filter an array of arrays based on values of an array
I have two arrays playerMoves
and movesList
. 我有两个数组
playerMoves
和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
. 我需要过滤
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#filter
, Array#every
和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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.