Merge default properties with properties of other object - Node.js Object

Node.js examples for Object:Property

Description

Merge default properties with properties of other object

Demo Code

'use strict';// w  ww .  ja  v  a2s .c  o  m

/**
 * merge default properties with properties of other object
 *
 * @param  {object} spec      user options object
 * @param  {object} defaults  default options object
 * @return {object} obj       overwritten object
 */
export default function (spec, defaults) {
    return Object.keys(defaults).reduce(function (obj, key) {
        obj[key] = spec && spec.hasOwnProperty(key) ? spec[key] : defaults[key];
        return obj;
    }, {});
}

Related Tutorials