🚀 Master JavaScript & Java with Ease!
Subscribe to Ram N Java for clean, beginner-friendly coding guides!
🔔 Subscribe to Ram N JavaIntroduction: Removing Properties from Nested JSON
When working with data in JavaScript, you often need to remove specific fields or keys that are no longer needed. If those fields are inside nested JSON objects, JavaScript makes it simple using the delete keyword.
In this tutorial, you'll learn how to target and delete any specific property from an inner nested object.
1. Sample Nested JSON Object
Let's take a look at a basic JSON structure with an inner object:
let myData = {
"name": "Ram",
"age": 30,
"cars": {
"car1": "Ford",
"car2": "BMW",
"car3": "Fiat"
}
};
Inside myData, the property cars holds another object containing three car items.
2. Deleting a Property Using the delete Keyword
To remove a property, place the delete operator before the target property path:
// Delete car2 from the cars object delete myData.cars.car2; // Or using bracket notation: // delete myData.cars["car2"];
3. Checking the Updated Object
After executing the delete command, car2 is completely removed from the object:
for (let key in myData.cars) {
console.log(myData.cars[key]);
}
// Output:
// Ford
// Fiat
Only Ford and Fiat remain because BMW (car2) was permanently deleted.
Key Takeaways
- Use
delete object.nestedObject.keyto remove a nested property. - The
deleteoperator mutates the original object in place. - Bracket notation like
delete object.nestedObject["key"]also works seamlessly.
Do you want to make your photo more stylish & attractive? Do you want to delete an unwanted object from your photo? If yes then use our great application clone Stamp for this purpose.
ReplyDeleteremove object