Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaScript: merge two objects
function merge(target, source) {
/* Merges two (or more) objects,
giving the last one precedence */
if ( typeof target !== 'object' ) {
target = {};
}
for (var property in source) {
if ( source.hasOwnProperty(property) ) {
var sourceProperty = source[ property ];
if ( typeof sourceProperty === 'object' ) {
target[ property ] = util.merge( target[ property ], sourceProperty );
continue;
}
target[ property ] = sourceProperty;
}
}
for (var a = 2, l = arguments.length; a < l; a++) {
merge(target, arguments[a]);
}
return target;
};
@solio

This comment has been minimized.

Copy link

solio commented Oct 16, 2014

how is the util.merge defined

@dixso

This comment has been minimized.

Copy link

dixso commented Feb 18, 2015

Remove "util."

@aepedraza

This comment has been minimized.

Copy link

aepedraza commented Feb 28, 2015

Thanks a lot, very useful

@petvas

This comment has been minimized.

Copy link

petvas commented Aug 25, 2015

line 17 util.merge

@noreliq

This comment has been minimized.

Copy link

noreliq commented Sep 25, 2015

great function. one comment: this also merges arrays, which may or may not be the desired behaviour.

you may want to include a check in if ( typeof sourceProperty === 'object' ) to make sure the object is also not an array.

@EvgenNechyporuk

This comment has been minimized.

Copy link

EvgenNechyporuk commented Jun 18, 2016

console.log(typeof null); - > "object"
console.log(typeof [1,2]); - > "object"
check only type enough

@JohnTheBeloved

This comment has been minimized.

Copy link

JohnTheBeloved commented Jun 22, 2016

Thanks, really Helped

@rsxdalv

This comment has been minimized.

Copy link

rsxdalv commented Aug 3, 2017

function merge(target: { [x: string]: {} }, source: { [x: string]: {} }) {

    /* Merges two (or more) objects,
       giving the last one precedence */

    if (typeof target !== "object") {
        target = {};
    }

    for (const property in source) {

        if (source.hasOwnProperty(property)) {

            const sourceProperty = source[property];

            if (typeof sourceProperty === "object" &&
                // tslint:disable-next-line:no-null-keyword
                sourceProperty !== null &&
                sourceProperty.constructor !== Array
            ) {
                target[property] = merge(target[property], sourceProperty);
                continue;
            }

            target[property] = sourceProperty;

        }

    }

    // tslint:disable-next-line:one-variable-per-declaration
    for (let a = 2, l = arguments.length; a < l; a++) {
        merge(target, arguments[a]);
    }

    return target;
}

// {} Ensures that there cannot be only 1 argument
merge({}, {}, ...sourceFixture);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.