Skip to content

Commit

Permalink
✨ IMPROVE: adding more examples to make the code more clear
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaban-Eissa committed Jun 7, 2024
1 parent 63e6a97 commit ea3613e
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions 63.create-cloneDeep.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,96 @@ Let's break down the selected code:
8. Finally, the function returns the clone.


#

### Real World Examples
```js
const array = [1, 2, 3, 4, 5];

const obj = {
a: 1,
b: {
c: 2,
d: 3,
},
};

const circular = {
a: 1,
};
circular.circular = circular;

const deep = {
a: 1,
b: {
c: 2,
d: 3,
},
e: {
f: {
g: 4,
},
},
};

const arrayClone = cloneDeep(array);
const objClone = cloneDeep(obj);
const circularClone = cloneDeep(circular);
const deepClone = cloneDeep(deep);

console.log(arrayClone); // [1, 2, 3, 4, 5]
console.log(objClone); // { a: 1, b: { c: 2, d: 3 } }
console.log(circularClone); // { a: 1, circular: [Circular] }
console.log(deepClone); // { a: 1, b: { c: 2, d: 3 }, e: { f: { g: 4 } } }

// The cloned object has a different reference.
console.log(array !== arrayClone); // true
console.log(obj !== objClone); // true
console.log(circular !== circularClone); // true
console.log(deep !== deepClone); // true

// The nested object has a different reference.
console.log(obj.b !== objClone.b); // true
console.log(deep.b !== deepClone.b); // true
console.log(deep.e !== deepClone.e); // true
console.log(deep.e.f !== deepClone.e.f); // true

// The circular reference is handled correctly.
console.log(circular === circular.circular); // true
console.log(circularClone === circularClone.circular); // true

// The cloned object has the same value.
console.log(array.toString() === arrayClone.toString()); // true
console.log(JSON.stringify(obj) === JSON.stringify(objClone)); // true
console.log(circular === circular.circular); // true
console.log(JSON.stringify(deep) === JSON.stringify(deepClone)); // true

// The cloned object has the same structure.
console.log(Object.keys(obj).toString() === Object.keys(objClone).toString()); // true
console.log(Object.keys(deep).toString() === Object.keys(deepClone).toString()); // true

// The cloned object has the same nested structure.
console.log(
Object.keys(obj.b).toString() === Object.keys(objClone.b).toString()
); // true
console.log(
Object.keys(deep.b).toString() === Object.keys(deepClone.b).toString()
); // true
console.log(
Object.keys(deep.e).toString() === Object.keys(deepClone.e).toString()
); // true
console.log(
Object.keys(deep.e.f).toString() === Object.keys(deepClone.e.f).toString()
); // true

// The cloned object has the same value.
console.log(obj.b.c === objClone.b.c); // true
console.log(deep.b.c === deepClone.b.c); // true
console.log(deep.e.f.g === deepClone.e.f.g); // true

```


#

### Reference
Expand Down

0 comments on commit ea3613e

Please sign in to comment.