mirror of
https://github.com/toss/es-toolkit.git
synced 2024-12-02 12:52:23 +03:00
b08e5c9bca
* feat: cloneDeep * test: cloneDeep * chore: add benchmark of cloneDeep * docs: add docs of cloneDeep * chore: fix name * fix: constructor type * feat: add index * chore: add doc * feat: Add native classes handling and benchmark structuredClone * feat: Resolved type and aligned behavior with structuredClone * fix: object init * fix: File for legacy nodejs --------- Co-authored-by: Sojin Park <raon0211@toss.im>
1.2 KiB
1.2 KiB
cloneDeep
Creates a deep copy of the given object.
Signature
function cloneDeep<T>(obj: T): T;
Parameters
obj
(T
): The object to be copied.
Returns
(T
): A deep copy of the given object.
Examples
const num = 29;
const clonedNum = cloneDeep(num);
console.log(clonedNum); // 29
console.log(clonedNum === num); // true
const arr = [1, 2, 3];
const clonedArr = cloneDeep(arr);
console.log(clonedArr); // [1, 2, 3]
console.log(clonedArr === arr); // false
const obj = { a: 1, b: 'es-toolkit', c: [1, 2, 3] };
const clonedObj = cloneDeep(obj);
console.log(clonedObj); // { a: 1, b: 'es-toolkit', c: [1, 2, 3] }
console.log(clonedObj === obj); // false
// Nested objects and arrays
const nestedObj = { a: { b: { c: 'deep' } }, d: [1, 2, { e: 'nested' }] };
const clonedNestedObj = cloneDeep(nestedObj);
console.log(clonedNestedObj); // { a: { b: { c: 'deep' } }, d: [1, 2, { e: 'nested' }] }
console.log(clonedNestedObj === nestedObj); // false
console.log(clonedNestedObj.a === nestedObj.a); // false
console.log(clonedNestedObj.d === nestedObj.d); // false
console.log(clonedNestedObj.d[2] === nestedObj.d[2]); // false