Algorithm
-
Create a Function for Cloning:
- Define a function, let's call it
cloneObject
, that takes an object as an input parameter.
- Define a function, let's call it
-
Check for Object Type:
- Ensure that the input is indeed an object. If not, return an error or handle the case appropriately.
-
Create a New Object:
- Initialize a new object, which will be the clone of the input object. You can do this by creating an empty object
{}
.
- Initialize a new object, which will be the clone of the input object. You can do this by creating an empty object
-
Iterate Through Properties:
- Use a loop (such as a
for...in
loop) to iterate through all the properties of the input object.
- Use a loop (such as a
-
Copy Properties:
- For each property, copy its key and value from the original object to the new object. You can achieve this using the assignment operator (
=
).
- For each property, copy its key and value from the original object to the new object. You can achieve this using the assignment operator (
-
Handle Nested Objects:
- If the value of a property is itself an object, recursively call the
cloneObject
function on that property to clone nested objects.
- If the value of a property is itself an object, recursively call the
-
Return the Cloned Object:
- Once all properties are copied, return the cloned object.
Code Examples
#1 Code Example- Clone the Object Using Object.assign()
Code -
Javascript Programming
// program to clone the object
// declaring object
const person = {
name: 'John',
age: 21,
}
// cloning the object
const clonePerson = Object.assign({}, person);
console.log(clonePerson);
// changing the value of clonePerson
clonePerson.name = 'Peter';
console.log(clonePerson.name);
console.log(person.name);
Copy The Code &
Try With Live Editor
Output
Peter
John
#2 Code Example- Clone the Object Using Spread Syntax
Code -
Javascript Programming
// program to clone the object
// declaring object
const person = {
name: 'John',
age: 21,
}
// cloning the object
const clonePerson = { ... person}
console.log(clonePerson);
// changing the value of clonePerson
clonePerson.name = 'Peter';
console.log(clonePerson.name);
console.log(person.name);
Copy The Code &
Try With Live Editor
Output
Peter
John
#3 Code Example with Javascript Programming
Code -
Javascript Programming
const person = {
name: 'John',
age: 21,
// the inner objects will change in the shallow copy
marks: { math: 66, english: 73}
}
// cloning the object
const clonePerson = { ... person}
console.log(clonePerson); // {name: "John", age: 21, marks: {…}}
// changing the value of clonePerson
clonePerson.marks.math = 100;
console.log(clonePerson.marks.math); // 100
console.log(person.marks.math); // 100
Copy The Code &
Try With Live Editor
#4 Code Example- Clone the Object Using JSON.parse()
Code -
Javascript Programming
// program to clone the object
// declaring object
const person = {
name: 'John',
age: 21,
}
// cloning the object
const clonePerson = JSON.parse(JSON.stringify(person));
console.log(clonePerson);
// changing the value of clonePerson
clonePerson.name = 'Peter';
console.log(clonePerson.name);
console.log(person.name);
Copy The Code &
Try With Live Editor
Output
Peter
John
Demonstration
JavaScript Programing Example to Clone a JS Object-DevsEnv