如何过滤数组中与 Javascript 中的搜索对象匹配的对象
How to filter object within an array that matches search object in Javascript
我正在尝试过滤和删除数组中的对象,但是删除的对象比我希望的要多
const people = [
{name: 'Adam', age: 30, country: 'USA'},
{name: 'Carl', age: 30, country: 'UK'},
{name: 'Bob', age: 40, country: 'China'},
];
const results = people.filter(element => {
// ️ using AND (&&) operator
return element.age !== 30 && element.name !== 'Carl';
});
console.log(results);
输出:
[ { name: 'Bob', age: 40, country: 'China' } ]
我希望只删除找到卡尔的对象
我想要的输出是
[ { name: 'Adam', age: 30, country: 'USA' }, { name: 'Bob', age: 40, country: 'China' } ]
如果您只需要移除 30yo Carls,则需要更新过滤条件
return !(element.age === 30 && element.name === 'Carl');
等于
return element.age !== 30 || element.name !== 'Carl';
我正在尝试过滤和删除数组中的对象,但是删除的对象比我希望的要多
const people = [
{name: 'Adam', age: 30, country: 'USA'},
{name: 'Carl', age: 30, country: 'UK'},
{name: 'Bob', age: 40, country: 'China'},
];
const results = people.filter(element => {
// ️ using AND (&&) operator
return element.age !== 30 && element.name !== 'Carl';
});
console.log(results);
输出:
[ { name: 'Bob', age: 40, country: 'China' } ]
我希望只删除找到卡尔的对象
我想要的输出是
[ { name: 'Adam', age: 30, country: 'USA' }, { name: 'Bob', age: 40, country: 'China' } ]
如果您只需要移除 30yo Carls,则需要更新过滤条件
return !(element.age === 30 && element.name === 'Carl');
等于
return element.age !== 30 || element.name !== 'Carl';