💾obj.mergeWith()

JSobjectbuilt-inObjectextension ⟩ obj.mergeWith(...sources)

Object.defineProperties(Object.prototype, {

    // 🔹 other related methods omitted ...
    
    // 🔹 obj.mergeWith(...sources)
    mergeWith: {
        value: function(...sources) {

            const descriptors = {};
            
            // collect all descriptors from sources
            sources.forEach(source => {
                
                // own enumerable (string-keyed) properties -> descriptors
                source.keys.forEach(key => {
                    descriptors[key] = source.propertyDescriptor(key);
                });
        
                // own enumerable (symbol-keyed) properties -> descriptors
                source.symbols.forEach(sym => {
                    const desc = source.propertyDescriptor(sym);
                    if (desc.enumerable) descriptors[sym] = desc;
                });
        
            });
        
            // merge
            return this.defineProperties(descriptors);
        },
    },

});

Last updated