How To Remove Specific JSON Object From Array JavaScript?
Last Updated :
18 Apr, 2024
Removing Specific JSON objects from the array is important because it allows for targeted data manipulation and maintenance within JavaScript applications. we will learn to Remove Specific JSON Object From Array JavaScript.
These are the following methods:
Using filter() method
In this approach, we are using the filter() method with a callback function that filters out objects from the jData array where the Name property is equal to "Trees", resulting in an array res containing objects excluding the specified one.
Syntax:
let res = array.filter(function(item) {
return condition;
});
Example: The below example uses the filter() method to Remove Specific JSON objects from Array JavaScript.
JavaScript
let jData = [{
"Name": "Trees",
"Course": "Introduction of Trees",
"Content": ["Binary Tree", "BST", "Generic Tree"]
},
{
"Name": "Graphs",
"Topics": ["BFS", "DFS", "Topological Sort"]
}
];
let res = jData.filter(obj => obj.Name !== "Trees");
console.log(res);
Output[ { Name: 'Graphs', Topics: [ 'BFS', 'DFS', 'Topological Sort' ] } ]
Using splice() method
In this approach, we are using the splice() method to remove an object from the jData array at the index found using findIndex(), making sure the deletion of the specified JSON object based on the "Name" property.
Syntax:
obj.splice(start, deleteCount, item1, item2, ...);
Example: The below example uses the splice() method to Remove Specific JSON objects from Array JavaScript.
JavaScript
let jData = [{
"Name": "Trees",
"Course": "Introduction of Trees",
"Content": ["Binary Tree", "BST", "Generic Tree"]
},
{
"Name": "Graphs",
"Topics": ["BFS", "DFS", "Topological Sort"]
}
];
let ind = jData.findIndex(obj => obj.Name === "Trees");
if (ind !== -1) {
jData.splice(ind, 1);
}
console.log(jData);
Output[ { Name: 'Graphs', Topics: [ 'BFS', 'DFS', 'Topological Sort' ] } ]
Using for loop
In this approach, we are using a for loop to iterate through the jData array, checking each object's "Name" property until finding the desired object ("Trees"). Once found, we use the splice() method to remove the object at the found index, making sure the deletion of the specified JSON object.
Syntax:
for (initialization; condition; increment/decrement) {
// code
}
Example: The below example uses a for loop to Remove Specific JSON objects from Array JavaScript.
JavaScript
let jData = [{
"Name": "Trees",
"Course": "Introduction of Trees",
"Content": ["Binary Tree", "BST", "Generic Tree"]
},
{
"Name": "Graphs",
"Topics": ["BFS", "DFS", "Topological Sort"]
}
];
let ind = -1;
for (let i = 0; i < jData.length; i++) {
if (jData[i].Name === "Trees") {
ind = i;
break;
}
}
if (ind !== -1) {
jData.splice(ind, 1);
}
console.log(jData);
Output[ { Name: 'Graphs', Topics: [ 'BFS', 'DFS', 'Topological Sort' ] } ]
Similar Reads
How to Remove Empty Object from JSON in JavaScript ? In JSON, empty objects can cause data inconsistency and processing issues. We will explore three different approaches filter method, forEach Loop, and for Loop to remove empty objects from JSON in JavaScript.Table of ContentUsing filter MethodUsing forEach LoopUsing for LoopUsing Array.reduce() Meth
3 min read
How to Remove Element from JSON Object in JavaScript ? In JavaScript, removing elements from a JSON object is important for modifying data structures dynamically. This object manipulation can help us to create dynamic web pages. The approaches to accomplish this task are listed and discussed below: Table of Content Using delete KeywordUsing filter Metho
2 min read
How to Remove a Specific Item from an Array in JavaScript ? Given an Array, the task is remove specific item from an array in JavaScript. It means we have an array with N items, and remove a particular item from array.ExamplesInput: Arr = [ 10, 20, 30, 40, 50, 60 ] removeItem = 40 Output: [ 10, 20, 30, 50, 60 ]Table of ContentUsing splice() MethodUsing filte
3 min read
How to Remove Null Objects from Nested Arrays in JavaScript ? Javascript arrays are a very flexible container that can hold both elements or values. Nevertheless, with nested arrays, it is crucial for data management and cleaning. Replacing null objects is a frequent demand.Below are the methods to remove null objects from nested arrays in JavaScript:Table of
9 min read
How to remove object from array of objects using JavaScript ? Removing an object from an array of objects in JavaScript refers to the process of eliminating a specific object from the array based on certain conditions, like matching a property value. This task is common in data manipulation, ensuring the array only contains the desired elements.There are two a
2 min read
How to get Values from Specific Objects an Array in JavaScript ? In JavaScript, an array is a data structure that can hold a collection of values, which can be of any data type, including numbers, strings, and objects. When an array contains objects, it is called an array of objects. Table of Content Using the forEach() methodUsing the map() methodUsing the filte
2 min read
How to remove Objects from Associative Array in JavaScript ? In this article, we are going to learn about removing Objects from Associative Array in Javascript, In JavaScript, you can remove objects from an associative array (also known as an object) using the following methods. Table of Content Using JavaScript delete operatorUsing JavaScript Array.filter()
4 min read
How to Remove a Property From JavaScript Object? The delete operator is used to remove a property from a JavaScript object. The delete operator allows you to remove a specified property from an object. Once a property is deleted, it no longer exists in the object.Using delete OperatorThe basic method to remove a property from a JavaScript object i
3 min read
How to Remove Duplicate Objects from an Array in JavaScript? In JavaScript, it's a common example that the arrays contain objects and there might be a possibility that the objects may or may not be unique. Removing these duplicate objects from the array and getting the unique ones is a common task in Web Development. These are the following approaches: Table
2 min read
How to remove a key-value pair from JavaScript object? Removing a key-value pair from an object in JavaScript means deleting a specific property and its corresponding value from the object. This can be achieved using the delete operator, destructuring with the rest operator, or various utility functions to manipulate the object dynamically.How to remove
3 min read