[英]How to filter object within an array that matches search object in Javascript
I am trying to filter and remove objects that are inside of my array however more objects are getting removed than I am hoping to target我正在尝试过滤和删除数组中的对象,但是删除的对象比我希望的要多
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);
outputs:输出:
[ { name: 'Bob', age: 40, country: 'China' } ]
I am hoping to only remove the object where Carl is found我希望只删除找到 Carl 的 object
My desired output would be我想要的 output 是
[ { name: 'Adam', age: 30, country: 'USA' }, { name: 'Bob', age: 40, country: 'China' } ]
You need to update the filter condition if you need to remove only 30yo Carls如果您只需要移除 30yo Carls,则需要更新过滤条件
return !(element.age === 30 && element.name === 'Carl');
which is equal to这等于
return element.age !== 30 || element.name !== 'Carl';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.