Given a JavaScript array containing objects in and the task is to delete certain objects from array of objects. There are two approaches to solve this problem which are discussed below:
Approach 1:
- Use array.forEach() method to traverse every object of the array.
- For each object use delete obj.property to delete the certain object element from array of objects.
Example: This example implements the above approach.
<!DOCTYPE HTML> <html> <head> <title> Remove certain property for all objects in array with JavaScript. </title> </head> <body style="text-align:center;"> <h1 style="color: green"> GeeksForGeeks </h1> <p id="GFG_UP"></p> <button onclick="gfg_Run()"> Click Here </button> <p id="GFG_DOWN" style="color:green;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var arr = [{ a: 'Val_1', b: 'Val_2' }, { a: 'Val_3', b: 'Val_4' }, { a: 'Val_1', b: 'Val_2' }]; el_up.innerHTML = "Click on the button to delete" + " certain property of object.<br>Array - " + JSON.stringify(arr); function gfg_Run() { arr.forEach(function(obj) { delete obj.a }); el_down.innerHTML = JSON.stringify(arr); } </script> </body> </html> |
chevron_right
filter_none
Output:
- Before clicking the button:

- After clicking the button:

Approach 2:
- Use array.map() method to traverse every object of the array.
- For each object use delete obj.property to delete the certain object from array of objects.
Example: This example implements the above approach.
<!DOCTYPE HTML> <html> <head> <title> Remove certain property for all objects in array with JavaScript. </title> </head> <body style="text-align:center;"> <h1 style="color: green"> GeeksForGeeks </h1> <p id="GFG_UP"></p> <button onclick="gfg_Run()"> Click Here </button> <p id="GFG_DOWN"></p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var arr = [{ a: 'Val_1', b: 'Val_2' }, { a: 'Val_3', b: 'Val_4' }, { a: 'Val_1', b: 'Val_2' }]; el_up.innerHTML = "Click on the button to delete" + " certain property of object.<br>Array - " + JSON.stringify(arr); function gfg_Run() { arr.map(function(obj) { delete obj.a; return obj; }); el_down.innerHTML = JSON.stringify(arr); } </script> </body> </html> |
chevron_right
filter_none
Output:
- Before clicking on the button:

- After clicking on the button:

Recommended Posts:
- How to remove duplicates from an array of objects using JavaScript ?
- Extract unique objects by attribute from array of objects.
- How to remove Objects from Associative Array in JavaScript ?
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- Sort array of objects by object fields in PHP
- How to access an object having spaces in the object's key using JavaScript ?
- JavaScript Course | Objects in JavaScript
- Sort an array of objects using Boolean property in JavaScript
- How to convert JSON string to array of JSON objects using JavaScript ?
- How to check a JavaScript Object is a DOM Object ?
- What is the difference between Object.keys() and Object.entries() methods in JavaScript ?
- How to remove a property from JavaScript object ?
- Remove blank attributes from a JavaScript Object
- How to check two objects have same data using JavaScript ?
- Sort array of objects by string property value in JavaScript
- Max/Min value of an attribute in an array of objects in JavaScript
- How to convert Object's array to an array using JavaScript ?
- How to merge properties of two JavaScript objects dynamically?
- Creating objects in JavaScript (4 Different Ways)
- JavaScript | Objects
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


