Last updated: Mar 1, 2024
Reading timeยท4 min
To remove an object from an array by its value:
Array.filter()
method to iterate over the array.filter()
method will return a new array that doesn't contain the
object.const arr = [{id: 1}, {id: 3}, {id: 5}]; const newArr = arr.filter(object => { return object.id !== 3; }); console.log(newArr) // ๐๏ธ [{id: 1}, {id: 5}]
The Array.filter() method doesn't mutate the contents of the original array, it returns a new array.
The function we passed to the filter()
method gets invoked with each object in
the array. If it returns a truthy value, the object is added to the new array.
const arr = [{id: 1}, {id: 3}, {id: 5}]; const newArr = arr.filter(object => { return object.id !== 3; }); console.log(newArr) // ๐๏ธ [{id: 1}, {id: 5}]
On each iteration, we check if the current object's id
property doesn't have a
specific value.
The new array contains all of the objects of the original array that meet the condition.
This is a two-step process:
Array.findIndex()
method to get the index of the object in the
array.Array.splice()
method to remove the object at that index.const arr = [{id: 1}, {id: 3}, {id: 5}]; const indexOfObject = arr.findIndex(object => { return object.id === 3; }); console.log(indexOfObject); // ๐๏ธ 1 arr.splice(indexOfObject, 1); console.log(arr); // ๐๏ธ [{id: 1}, {id: 5}]
The function we passed to the Array.findIndex() method gets called with each element (object) in the array until it returns a truthy value or iterates over the entire array.
In the example, we got the index of the first object in the array that has an
id
property with a value of 3
.
const arr = [{id: 1}, {id: 3}, {id: 5}]; const indexOfObject = arr.findIndex(object => { return object.id === 3; }); console.log(indexOfObject); // ๐๏ธ 1
The next step is to use the Array.splice() method to remove the object at that index from the array.
The arguments we passed to the Array.splice()
method are:
const arr = [{id: 1}, {id: 3}, {id: 5}]; const indexOfObject = arr.findIndex(object => { return object.id === 3; }); console.log(indexOfObject); // ๐๏ธ 1 arr.splice(indexOfObject, 1); console.log(arr); // ๐๏ธ [{id: 1}, {id: 5}]
The findIndex()
method returns -1
if the condition is never met.
-1
as a start index to the Array.splice()
method, it would remove the last element from the array, which would be a bug in our program.If this is a possible scenario in your code, use the Array.filter()
method
instead.
The splice
method changes the contents of the original array, which can be
confusing and difficult to follow. Most of the time it's better to create a new
array, containing only the elements that we need.
The Array.filter()
method is different from the findIndex()
and splice()
approach for a couple of reasons:
The findIndex()
method returns the index of the first element that meets
the condition, whereas the filter()
method might filter out multiple
elements with the same value.
The splice()
method changes the contents of the original array, whereas the
filter()
method creates a new array.
Alternatively, you can use the Array.slice()
method.
This is a two-step process:
Array.findIndex()
method to get the index of the object in the
array.Array.slice()
method to create a new array excluding the object at
that index.const arr = [{id: 1}, {id: 3}, {id: 5}]; const indexOfObject = arr.findIndex(object => { return object.id === 3; }); const newArr = [ ...arr.slice(0, indexOfObject), ...arr.slice(indexOfObject + 1), ]; // ๐๏ธ [ { id: 1 }, { id: 5 } ] console.log(newArr);
We used the Array.findIndex()
method to get the index of the object in the
array.
The method returns the index of the object that has an id
property with a
value of 3
.
Array.slice()
method to create a new array that doesn't contain the object at that index.The spread syntax (...) is used to unpack the values of the array slices into a new array.
We passed the following arguments to the Array.slice() method:
We added 1
to the start index in the second call to the Array.slice()
method
to exclude the object from the new array.
const arr = [{id: 1}, {id: 3}, {id: 5}]; const indexOfObject = arr.findIndex(object => { return object.id === 3; }); const newArr = [ ...arr.slice(0, indexOfObject), ...arr.slice(indexOfObject + 1), ]; // ๐๏ธ [ { id: 1 }, { id: 5 } ] console.log(newArr);
The Array.slice()
method doesn't mutate the original array.
The newArr
variable contains all objects of the original array excluding the
object that has an id
property with a value of 3
.
You can learn more about the related topics by checking out the following tutorials: