Merge Object - Node.js Object

Node.js examples for Object:Object Operation

Description

Merge Object

Demo Code


//takes n objects, and merges them into one super-object, which'll one day rule
// the galaxy. Non-mutative. The merging, not the galaxy ruling.
///*ww  w.  j a  va  2 s  .  c om*/
// > Object.merge( {a : 4, b : 5}, {a : 6, c : 7} )
// { a : 6, b : 5, c : 7 }
Object.merge = function () {
  return [].reduce.call( arguments, function ( ret, merger ) {

    Object.keys( merger ).forEach(function ( key ) {
      ret[ key ] = merger[ key ];
    });

    return ret;
  }, {} );
};

Related Tutorials