Delete Operation
Delete operation refers to the removal of an object property. It can be done as follows:
- Using Delete operator: Delete keyword is used to remove the object property mentioned after it.
- Using Destructuring Assignment: It refers to the JavaScript syntax for unpacking the object values and assigning them to other variables.
Syntax
delete obj.propety or delete obj['property'] // Using delete operator
const {property1, newObj} = oldObj; // Using destructuring
Example: This example describes the deletion of the Object & its associated properties & values.
Javascript
const obj = { name: 'xyz' , age: 52, city: 'delhi' } // Display original object console.log( "Original object: " , obj); // Using destructuring const { age, ...newObj } = obj; console.log( "New object without age attribute: " , newObj); // Using delete operator delete obj.city; console.log( "Original object after deleting city: " , obj); |
Original object: { name: 'xyz', age: 52, city: 'delhi' } New object without age attribute: { name: 'xyz', city: 'delhi' } Original object after deleting city: { name: 'xyz', age: 52 }
How to Perform CRUD Operations on JavaScript Object ?
This article will demonstrate the CRUD Operations on a JavaScript Object. The operations are Create, Read, Update, and Delete. With these operations, we can create, take input, manipulate & destroy the objects.
JavaScript Objects are a collection of keys, values or properties/attributes, and entries. The values can be any data type as well as JavaScript functions, arrays, etc.
Syntax
// Creating an Object
const rectangle = {
length: 25,
width: 20,
area: function(){
return this.length*this.width;
}
}