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