EN
Javascript - array includes() method opposite
1
answers
0
points
What is the correct way of removing objects that have a certain property?
I can use includes()
in the followind way:
const array = [
{ id: 1, name: 'Tom1' },
{ id: 2, name: 'Ann' },
{ id: 3, name: 'Tom2' },
{ id: 4, name: 'Kate' },
];
const result = array.filter((item) => item.name.includes('Tom'));
To get the result:
[ { id: 1, name: 'Tom1' }, { id: 3, name: 'Tom2' } ]
But how can I do the opposite to remove the objects above, keeping everything but them?
1 answer
0
points
Just negate the result of the function.
// ONLINE-RUNNER:browser;
const array = [
{ id: 1, name: 'Tom1' },
{ id: 2, name: 'Ann' },
{ id: 3, name: 'Tom2' },
{ id: 4, name: 'Kate' },
];
const result = array.filter((item) => !item.name.includes('Tom'));
console.log(JSON.stringify(result, null, 4)); // [ { id: 2, name: 'Ann' }, { id: 4, name: 'Kate' } ]
References
0 comments
Add comment