How to Perform CRUD Operations on JavaScript Object ?
Last Updated :
12 Mar, 2024
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;
}
}
- Create: Create a new object or add an object property to an existing object.
- Read: Read any object existing property property
- Update: Update or modify an object or the attribute values.
- Delete: Delete or remove an entry from an object or the whole object.
We will explore each operation & will understand the associated different methods available in JavaScript to accomplish this task.
Create Operation
This operation refers to the creation of an object or adding a new property to it. The following methods can be used to create the Object:
- Using Object Constructor: It is a function that is used to create and initialize an object. The return value is assigned to a variable. The variable contains a reference to the new object.
- Using Object literals: It is the list of key-value pairs separated by a comma “, “ for creating an object.
Syntax
// Using Object Constructor
const obj = new Object();
// Using Object Literals
cosnt obj = {
key1 : value1
}
Example: In this example, we will create an object using the above two methods.
Javascript
const rectangle = {
length: 25,
width: 20,
area: function () {
return this .length * this .width
}
}
const square = new Object();
square.side = 10;
square.area = function () {
return this .side * this .side
};
square.side = 20;
console.log(rectangle, "area: " + rectangle.area());
console.log(square, "area: " + square.area());
|
Output
{ length: 25, width: 20, area: [Function: area] } area: 500
{ side: 20, area: [Function (anonymous)] } area: 400
Read Operation
The read operation refers to reading and accessing the properties and values of an object. It can be done using the following methods:
- Using Destructuring Assignment: It refers to the JavaScript syntax for unpacking the object values and assigning them to other variables.
- Using Dot Notations: This syntax allows to access the object properties and items using a [dot] or “. ” symbol.
- Using Bracket Notation: It allows to access the object property using the property name inside the brackets “[ ]”
Syntax
console.log(obj.property1) // Usign dot notation
console.log(obj.property1.subProperty) // For Nested objects
console.log(obj['property2']) // Using Square Brackets
const { key1 } = obj1; // Using Object Destructuring
console.log(key1);
Example: In this example, we will use different methods to read object property values.
Javascript
const rectangle = {
length: 25,
width: 20,
area: function () {
return this .length * this .width
}
}
const { length } = rectangle
console.log( "Rectangle length: " + length);
console.log( "Rectangle width: " + rectangle[ 'width' ]);
console.log( "Rectangle area: " + rectangle.area());
|
Output
Rectangle length: 25
Rectangle width: 20
Rectangle area: 500
Update Operation
It means to access and modify an object’s properties. It can be done using these methods:
- Using Destructuring Assignment: It refers to the JavaScript syntax for unpacking the object values and assigning them to other variables.
- Using Dot Notations: This syntax allows to access the object properties and items using a [dot] or “. ” symbol.
Syntax
obj.property1 = "newValue"; // Using dot notation
obj['property2'] = "value2"; // Using brackets
Example: This example describes the manipulation of the Object properties & their associated values.
Javascript
const rectangle = {
length: 25,
width: 20,
area: function () {
return this .length * this .width;
}
}
console.log( "Original Area: " + rectangle.area());
rectangle.length = 50;
rectangle[ 'width' ] = 40;
console.log( "Original Area: " + rectangle.area());
|
Output
Original Area: 500
Original Area: 2000
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'
}
console.log( "Original object: " , obj);
const { age, ...newObj } = obj;
console.log( "New object without age attribute: " , newObj);
delete obj.city;
console.log( "Original object after deleting city: " , obj);
|
Output
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 }
Similar Reads
How to read properties of an Object in JavaScript ?
Objects in JavaScript, it is the most important data type and forms the building blocks for modern JavaScript. These objects are quite different from JavaScriptâs primitive data-types(Number, String, Boolean, null, undefined, and symbol) in the sense that these primitive data-types all store a singl
2 min read
How to remove a property from JavaScript object ?
The delete operator is used to remove a property from a JavaScript object. The delete operator allows you to remove a specified property from an object. Once a property is deleted, it no longer exists in the object. Using delete OperatorThe basic method to remove a property from a JavaScript object
3 min read
How to print the content of an object in JavaScript ?
To print the content of an object in JavaScript we will use JavaScript methods like stringify, object.values and loops to display the required data. Let's first create a JavaScript Object containing some key-values as given below: C/C++ Code // Given Object const obj = { name: 'John', age: 30, city:
3 min read
How to transform JSON text to a JavaScript object ?
JSON (JavaScript Object Notation) is a lightweight data-interchange format. As its name suggests, JSON is derived from the JavaScript programming language, but itâs available for use by many languages including Python, Ruby, PHP, and Java, and hence, it can be said as language-independent. For human
3 min read
How to Pass Object as Parameter in JavaScript ?
We'll see how to Pass an object as a Parameter in JavaScript. We can pass objects as parameters to functions just like we do with any other data type. passing objects as parameters to functions allows us to manipulate their properties within the function. These are the following approaches: Table of
3 min read
How to add and remove properties from objects in JavaScript ?
We will try to understand how to add properties to an object and how to add or remove properties from an object in JavaScript. Before we go and see the addition and removal of properties from an object let us first understand the basics of an object in JavaScript. Object: An object in JavaScript is
6 min read
How to get the size of a JavaScript object ?
In this article, we will see the methods to find the size of a JavaScript object. These are the following ways to solve the problem: Table of Content Using Object.keys() methodUsing Object.objsize() methodUsing Object.entries() methodUsing Object.values() methodUsing Object.keys() methodWe can get t
2 min read
How to get property descriptors of an object in JavaScript ?
Here, we are going to discuss the property descriptors of an Object in JavaScript. The Object.getOwnPropertyDescriptor() method returns an object describing a specific property on a given object. A JavaScript object can be created in many ways and its properties can be called by using property descr
2 min read
How to Serialize Complex Objects in JavaScript ?
Serialize Complex Objects in JavaScript refers to the process of converting complex JavaScript objects into a format that can be easily stored or transmitted, typically as a string, such as JSON (JavaScript Object Notation). Table of Content Using JSON.stringify()Using Third-Party LibrariesUsing JSO
2 min read
How to Convert String of Objects to Array in JavaScript ?
This article will show you how to convert a string of objects to an array in JavaScript. You have a string representing objects, and you need to convert it into an actual array of objects for further processing. This is a common scenario when dealing with JSON data received from a server or stored i
4 min read