How to delete multiple objects from an array?
Currently I have
let arry1 = [
{
id:0,
name:'My App',
another:'thing',
},
{
id:1,
name:'My New App',
another:'things'
},
{
id:2,
name:'My New App',
another:'things'
}
];
Then I have an indexes array like this
let arry2 = [1, 2]; // Indexes to delete
finally the result has to be:
let arry1 = [{
id:0,
name:'My App',
another:'thing',
}]
You can use filter
. It expects an predicate and will return an elemtent if the predicate returns true
. I used excludes
as predicate and it will return false
if the current index
is inside the indicesToRemove
.
objects.filter((object, index) => excludesIndicesToRemove(index))
const objects = [{
id: 0,
name: 'My App',
another: 'thing',
},
{
id: 1,
name: 'My New App',
another: 'things'
},
{
id: 2,
name: 'My New App',
another: 'things'
}
]
const indicesToRemove = [1, 2]
const not = bool =>
!bool
const includes = xs => x =>
xs.includes(x)
const excludes = xs => x =>
not(includes(xs)(x))
const excludesIndicesToRemove = excludes(indicesToRemove)
console.log(
objects.filter((object, index) => excludesIndicesToRemove(index))
)