ts-utils
  • Getting Started
  • Contributing
  • Testing
  • Functions
    • Array Methods
      • isArr
      • isArrEmpty
      • pushUniqueValue
      • pushOrUpdate
    • Date Methods
      • toNow
      • formatDate
      • fromNow
    • Number Methods
      • toFixed
      • parseNumber
      • parseFormat
      • addUnit
      • addSeparators
      • formatNumber
      • ensureNumber
      • numberEmptyState
    • Object Methods
      • parseJSON
      • deepClone
      • parseNumValues
      • removeEmptyKeys
      • sanitizeQuery
      • removeKeys
      • renameKey
      • renameKeys
      • sortByKeyLength
      • querylize
      • parseNumericObj
      • reserveKeys
      • hasKeysAndValues
    • String Methods
      • randomStr
      • toUpperCase
      • toLowerCase
      • toFullUrl
      • initials
      • generateUuid
Powered by GitBook
On this page
  • deepClone(obj)
  • Arguments
  • Returns
  • Example
  • Usage
  • Notes

Was this helpful?

  1. Functions
  2. Object Methods

deepClone

deepClone(obj)

Creates a deep copy of the input object, including all nested objects and arrays. It ensures that the cloned object is independent of the original, allowing for safe manipulation without affecting the original data structure.

Arguments

  • obj (T | Record<string, any> | null): The object to be deep cloned. It can be of type T, a generic object type, or null.

Returns

  • T: A deep copy of the input object.

Example

typescript
import { deepClone } from 'check-engineering/ts-utils';

const originalObject = { name: 'Alice', age: 30, hobbies: ['reading', 'painting'] };
const clonedObject = deepClone(originalObject); // => { name: 'Alice', age: 30, hobbies: ['reading', 'painting'] }

Usage

typescript
import { deepClone } from 'check-engineering/ts-utils';

const data = { key: 'value', nested: { subkey: 'subvalue' } };
const clonedData = deepClone(data); // => { key: 'value', nested: { subkey: 'subvalue' } }

Notes

  • The deepClone function is essential for creating independent copies of complex objects, ensuring that changes made to the cloned object do not affect the original.

  • It recursively clones nested objects and arrays, providing a complete replica of the input object.

  • This function is valuable for scenarios where data immutability and preservation of the original structure are crucial.

PreviousparseJSONNextparseNumValues

Last updated 11 months ago

Was this helpful?