Written by Ian S. Pringle

Created on 2022-7-4 @ 0:0

Last updated on 2022-7-4 @ 0:0

This is my deep-merge function to merge an infinite number of objects.

function merge(...objs) {
    const newObj = {};
    const isObject = obj => typeof obj == 'object' && obj !== null;

    if (objs.some(obj => Array.isArray(obj))) {
      return [].concat(...objs.filter(Array.isArray));
    }
    while (objs.length > 0) {
        let obj = objs.splice(0, 1)\[0\];
        if (isObject(obj)) {
            for (let key in obj) {
            if (obj.hasOwnProperty(key)) {
                if (isObject(obj[key])) {
                  newObj[key] = merge(newObj[key] || {}, obj[key]);
                } else {
                  newObj[key] = obj[key];
                }
            }
            }
        }
    }
    return newObj;
}