Open In App

How To Remove Specific JSON Object From Array JavaScript?

Last Updated : 18 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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' ] } ]


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads