Remove Array Element Based on Object Property in JavaScript
Last Updated :
15 Jan, 2025
Here are the several methods that can be used to remove array elements based on object property in JavaScript
1. Using the filter() method
The filter() method is useful if you want to preserve the original array and create a new one without the element(s) you want to remove.
JavaScript
let a = [{ id: 1, name: 'Aahana' },{ id: 2, name: 'Neha' },{ id: 3, name: 'Charu' }];
let up = a.filter(a => a.id !== 2);
console.log(up);
Output[ { id: 1, name: 'Aahana' }, { id: 3, name: 'Charu' } ]
In this example
- Input Array: people(a) contains objects with id and name properties.
- Filter Method: The filter() method creates a new array, including only objects where person.id is not 2.
2. Using splice() method
The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
JavaScript
let a = [{ id: 1, name: 'Aahan' }, { id: 2, name: 'Neha' }, { id: 3, name: 'Charu' }];
let rem = a.findIndex(a => a.id === 2);
if (rem !== -1) {
a.splice(rem, 1);
}
console.log(a);
Output[ { id: 1, name: 'Aahan' }, { id: 3, name: 'Charu' } ]
In this example
- findIndex(): Finds the index of the object where id === 2. If found, it returns the index; otherwise, it returns -1.
- Condition: Checks if the index is valid (!== -1).
- splice(): Removes the element at the found index directly from the original array.
3. forEach()
The forEach() method executes a provided function once for each array element.
JavaScript
let a = [{id: 1,name: 'Luvkush'}, {id: 2, name: 'Jenni'},{id: 3,name: 'Daya'}];
for (let i = 0; i < a.length; i++) {
if (a[i].id === 2) {
a.splice(i, 1);
i--;
}
}
console.log(a);
Output[ { id: 1, name: 'Luvkush' }, { id: 3, name: 'Daya' } ]
In this example
- The code removes the object with id: 2 from the array using a loop and splice, adjusting the index with i-- to avoid skipping elements.
4. Using reduce() method
The reduce() method in JavaScript processes an array to produce a single output by applying a callback function to each element.
JavaScript
let arr = [{ id: 1, name: 'Tanu' }, { id: 2, name: 'Jayant' }, { id: 3, name: 'Riya' },];
let id = 2;
arr = arr.reduce((acc, item) => {
if (item.id !== id) {
acc.push(item);
}
return acc;
}, []);
console.log(arr);
Output[ { id: 1, name: 'Tanu' }, { id: 3, name: 'Riya' } ]
In this example
- The code removes an object with id = 2 from the array using the reduce() method.
- It iterates through the array, pushing only the objects that don’t match the specified id into a new array, which becomes the updated arr.
5. Using findIndex() Method with splice()
The findIndex() method is used to locate the index of the element that matches the specific property value. Once the index is found, we use the splice() method to remove the element from the array.
JavaScript
let a = [{ id: 1, name: 'Jaya' },{ id: 2, name: 'Raha' },{ id: 3, name: 'Daya' }];
const id = 2;
const index = a.findIndex(element => element.id === id);
if (index !== -1) {
a.splice(index, 1);
}
console.log(a);
Output[ { id: 1, name: 'Jaya' }, { id: 3, name: 'Daya' } ]
In this example
- The code removes the object with id = 2 from the array.
- It uses findIndex() to locate the index of the matching object and, if found (index !== -1), removes it using splice().
6. Using map() method
The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.
JavaScript
let a = [{ id: 1, name: 'Jaya' },{ id: 2, name: 'Jenni' },{ id: 3, name: 'Bhavya' },];
const id = 2;
a = a.map(item => {
return item.id !== id ? item : null;
}).filter(item => item !== null);
console.log(a);
Output[ { id: 1, name: 'Jaya' }, { id: 3, name: 'Bhavya' } ]
In this example
- The code removes the object with id = 2 from the array.
- It uses map() to replace the matching object with null, then filters out null values using filter(), leaving the remaining objects in the array.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read