Skip to content

Clone object with prototype ,and deeply merge (deep copy) properties of two JS objects.

License

Notifications You must be signed in to change notification settings

riversun/merge-deeply

Repository files navigation

merge-deeply

npm version CircleCI codecov Codacy Badge

Allow you to Merge or Clone with prototypes

  • You can deep merge (deep copy) the properties of an object

  • An object with a prototype declaration can be cloned to include the prototype.

  • Objects that have prototype declarations, prototypes and properties that can be deep copied

Usage

Install

Use with NPM

npm install merge-deeply

Use on browser from CDN

<script src="https://cdn.jsdelivr.net/npm/merge-deeply@2.0.1/lib/merge-deeply.js">

Usage

Cloning an object that has a prototype declaration

The following will execute a copy of the method declared as the prototype and a deep copy of all the properties.

const mergeDeeply = require('merge-deeply');

const clonedObject=mergeDeeply({op:'clone',object1:obj1});

Merging 2 objects that has a prototype declaration

The two objects will be merged and a new object will be created, as follows Of course the prototype is copied.

const mergeDeeply = require('merge-deeply');

const mergedObject=mergeDeeply({op:'merge',object1:obj1,object2:obj2});

Copy(overwrite) properties into object

The following merges obj1 with obj2's properties

const mergeDeeply = require('merge-deeply');

mergeDeeply({op:'overwrite-merge',object1:obj1,object2:obj2});

Merging 2 "Properties only" Objects

If the "concatArray" is true, elements of the array are added at merge time

const mergeDeeply = require('merge-deeply');

const a = {
    foo: 'myValue',
    bar: {
        barKey1: 'myValue1',
        barKey2: 'myValue2'
    },
    myArray: [{arrKey1: 'arrValue1'}, {arrKey2: 'arrValue2'}]

};

const b = {
    foo: 'updatedMyValue',
    bar: {
        barKey1: 'updatedMyValue1',
        barKey2: 'myValue2'
    },
    some: {someKey: 'someValue'},
    myArray: [{arrKey3: 'arrValue3'}, {arrKey4: 'arrValue4'}]

};


const result = mergeDeeply({op:'merge',object1:a,object2:b, concatArray: true});
console.log('result=');
console.log(result);

When you run it you will get the following result.

result=
{ foo: 'updatedMyValue',
  bar: { barKey1: 'updatedMyValue1', barKey2: 'myValue2' },
  myArray: 
   [ { arrKey1: 'arrValue1' },
     { arrKey2: 'arrValue2' },
     { arrKey3: 'arrValue3' },
     { arrKey4: 'arrValue4' } ],
  some: { someKey: 'someValue' } }

About

Clone object with prototype ,and deeply merge (deep copy) properties of two JS objects.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published