这给了我false
:
var arr = [];
for (y = 0; y < 3; y++) {
for (x = 0; x < 3; x++) {
arr.push({y: y, x: x});
}
}
console.log(arr.includes({y: 1, x: 2}));
我试图切片数组中对象的值,但它仍然是false
:
var arr_ = [];
arr.forEach(function(e) {
arr_.push(Object.values(e));
});
console.log(arr_.includes([1, 2]));
就我而言,我想用一个对象数组过滤另一个对象:
var el = [{y: 1, x: 1}, {y: 1, x: 2}];
var filteredArr = [];
arr.forEach(function(e) {
if (!el.includes(e)) filteredArr.push(e);
});
希望,有一种简单的方法可以使此过滤器起作用:)
答案 0 :(得分:1)
您可以使用Array.filter和Array.some:
var arr = [];
for (y = 0; y < 3; y++) {
for (x = 0; x < 3; x++) {
arr.push({y: y, x: x});
}
}
var el = [{y: 1, x: 1}, {y: 1, x: 2}];
var filtered = arr.filter(a =>
el.some(b => a.x === b.x && a.y === b.y)
);
console.log(filtered)