Remove an object from an array of objects in JavaScript

In this tutorial, I will show you how you can remove an object from an array of objects in JavaScript. An array of objects in JavaScript basically means an array that contains objects as its elements. Below are two methods for this purpose.

Using filter() method

The filter() method creates a new array with all elements that pass the test implemented by the provided function. We will use this method to create a new array by removing a specific object from the array.

Here is the program below to remove an object from an array of objects:

const array = [
  {name: 'Alice', age: 23}, 
  {name: 'Emi', age: 27}, 
  {name: 'Jone', age: 29}
];

// Remove the object by name property value
const newArray = array.filter(obj => obj.name !== "Emi");

console.log(newdArray);

In the above program, I have created an array that contains three objects.

Then I have used filter() method that iterates over each object in the array and returns a new array containing only the objects whose name property is not equal to Emi.

Output:

[ { name: 'Alice', age: 23 }, { name: 'Jone', age: 29 } ]

As you can see in the output, the above program removed the object whose name property is equal to Emi and returned a new array.

You can also remove an object from the above array of objects by its age property like below.

const newArray = array.filter(obj => obj.age !== 27);

 

Using splice() method

This method allows us to add elements to an array, remove elements from an array or add elements or remove elements simultaneously. The original array can be overwritten by this method.

Here is the program below to remove an object from an array of objects:

const array = [
  {name: 'Alice', age: 23}, 
  {name: 'Emi', age: 27}, 
  {name: 'Jone', age: 29}
];

// Remove the object by name property value
const index = array.findIndex(obj => obj.name === "Emi");
array.splice(index, 1);

console.log(array);

In the above program, I have used findIndex() method to find the index of the object which I want to remove by name property value (Emi) in the array.

Then I have used splice() method to remove the object at that index by specifying the index inside it. I have also passed 1 inside this method that represents the number of elements to remove.

Output:

[ { name: 'Alice', age: 23 }, { name: 'Jone', age: 29 } ]

 

If you wish, you can remove an object from the above array of objects by its age property as shown below:

const index = array.findIndex(obj => obj.age === 27);

 

Here is another example below to remove an object from an array of objects.

const array = [
  {name: 'Alice', age: 23}, 
  {name: 'Emi', age: 27}, 
  {name: 'Jone', age: 29}
];

array.splice(1, 1);

console.log(array);

In the above program, I have passed two arguments inside the splice() method. The first one is 1 specifies the index of the second object which means the second object will be removed from the array.

And the other one is also 1 represents the number of elements to remove.

Output:

[ { name: 'Alice', age: 23 }, { name: 'Jone', age: 29 } ]

Leave a Reply

Your email address will not be published. Required fields are marked *