JavaScript | Remove a JSON attribute
The task is to remove a JSON attribute from the JSON object. To do so, Here are few of the mostly used techniques discussed.
First delete property need to be discussed.
-
Delete property
This keyword deletes a property from an object:- This keyword deletes both the value of the property and the property itself.
- After deletion, the property is not available for use before it is added back again.
- This operator is created to be used on object properties, not on variables or functions.
- This operator should not be used on predefined JavaScript object properties. It can crash your application.
Syntax:
delete object.property or delete object['property']
Parameters:
- object: It specifies the name of an object, or an expression evaluating to an object.
- property: .It specifies the property to delete.
Return value:
It returns true for all cases and returns false when the property is an own non-configurable property.
Example 1: This example deletes the prop_12 from the myObj object via var key by using delete property.
<!DOCTYPE HTML> <html> <head> <title> JavaScript | Remove a JSON attribute. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 16px; font-weight: bold;"> </p> <button onclick="gfg_Run()"> Click here </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var myObj = { 'prop_1': { 'prop_11': 'value_11', 'prop_12': 'value_12' } }; el_up.innerHTML = JSON.stringify(myObj); function gfg_Run() { var key = "prop_12"; delete myObj.prop_1[key]; el_down.innerHTML = JSON.stringify(myObj); } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example 2: This example deletes the prop_11 from the myObj object by using delete property.
<!DOCTYPE HTML> <html> <head> <title> JavaScript | Remove a JSON attribute. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 16px; font-weight: bold;"> </p> <button onclick="gfg_Run()"> Click here </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var myObj = { 'prop_1': { 'prop_11': 'value_11', 'prop_12': 'value_12' } }; el_up.innerHTML = JSON.stringify(myObj); function gfg_Run() { delete myObj.prop_1.prop_11; el_down.innerHTML = JSON.stringify(myObj); } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- JavaScript | Add new attribute to JSON object
- JavaScript JSON
- Javascript | JSON PHP
- JavaScript | JSON Arrays
- JavaScript | JSON HTML
- JavaScript | JSON parse() Method
- Deserializing a JSON into a JavaScript object
- How to convert PHP array to JavaScript or JSON ?
- JavaScript | Convert an array to JSON
- JavaScript | JSON.stringify() with Examples
- JavaScript | How to add an element to a JSON object?
- Converting JSON text to JavaScript Object
- How to send a JSON object to a server using Javascript?
- How to pretty print JSON string in JavaScript ?
- JavaScript | Check if a key exists inside a JSON object
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.



