Allows deep cloning of plain objects that contain primitives, nested plain objects, or nested plain arrays.
npm install object-assign-deepobjectAssignDeep() now mutates the first argument in the same way Object.assign() does.Do not use this module if:
* Your objects are (or contain) native objects such as Date (nested Array is fine).
* Your objects contain circular references (you'll cause a stack overflow).
* Your objects are instances of some class you've written.
* You are concerned with prototype chains, property descriptors, unenumerable properties, and any other advanced uses.
If you need to do something fancy like the above you'll need to write a custom solution for your use case.
``javascriptobject-assign-deep
const objectAssignDeep = require();
const mergedObjects = objectAssignDeep(target, object1, object2, ...objectN);
const clonedObject = objectAssignDeep({}, originalObject);
`
Simples!
directory for a few examples, including one example case that demonstrates why you can't get clever with object cloning.`javascript
const objectAssignDeep = require(object-assign-deep);const objectA = {
prop1:
Hello,
prop2: World,
nested: {
bool: true,
super: 123,
still: here!,
},
array1: [1, 2, 3],
array2: [4, 5, 6],
};const objectB = {
prop2:
Universe,
name: Josh,
nested: {
bool: false,
},
array1: null,
};const objectC = {
location:
United Kingdom,
name: Bob,
nested: {
super: 999,
},
array2: [100, 101, 102],
};const result = objectAssignDeep(objectA, objectB, objectC);
console.log(
Result:, result);/*
* {
* prop1: 'Hello',
* prop2: 'Universe',
* nested: { bool: false, super: 999, still: 'here!' },
* array1: null,
* array2: [100, 101, 102],
* name: 'Bob',
* location: 'United Kingdom'
* }
*/
`API Overview
$3
Merges all the objects together mutating the target` in the process and returning the result.| Option | Default Value | Description |
|----------------|---------------|-------------|
| arrayBehaviour | "replace" | By default arrays in later objects will overwrite earlier values, but you can set this to "merge" if you want to concatenate the arrays instead. |
If you need more customisation options please take a look at the Object-Extender module which builds upon Object-Assign-Deep.