Algorithm


  1. Create a Function for Cloning:

    • Define a function, let's call it cloneObject, that takes an object as an input parameter.
  2. Check for Object Type:

    • Ensure that the input is indeed an object. If not, return an error or handle the case appropriately.
  3. 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 {}.
  4. Iterate Through Properties:

    • Use a loop (such as a for...in loop) to iterate through all the properties of the input object.
  5. 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 (=).
  6. 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.
  7. 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

x
+
cmd
{name: "John", age: 21}
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

x
+
cmd
{name: "John", age: 21}
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

x
+
cmd
{name: "John", age: 21}
Peter
John
Advertisements

Demonstration


JavaScript Programing Example to Clone a JS Object-DevsEnv

Previous
JavaScript Practice Example #3 - Assign 3 Variables and Print Good Way