TehShrike / deepmerge

A library for deep (recursive) merging of Javascript objects
MIT License
2.75k stars 216 forks source link

Merge plain objects within Array #196

Closed iniatse closed 4 years ago

iniatse commented 4 years ago
var deepmerge = require('deepmerge');
var isPlainObject = require('is-plain-object');

const target = {
    'series': [
        {
            'innerSize': '75%',
        }
    ]
};

const source = {
    'series': [
        {
            'data': 'bla'
        }
    ]
}

var obj = deepmerge(target, source, {
    'isMergeableObject': isPlainObject
})

console.log(obj);

Is it somehow possible at the moment to get the series[0] object merged such that innerSize and data is available on it, while still having isMergeableObject defined?

iniatse commented 4 years ago

Oh sry it is answered within the documentation. Use 'arrayMerge': combineMerge, but sadly this does not work in conjunction with 'isMergeableObject': isPlainObject

iniatse commented 4 years ago

I used thw following instead of isPlainObject, then the problem from the last comment also got solved.

function isMergeableObject(o) {
    return isPlainObject(o) || o.constructor.name === 'Array';
}