Combine or merge JSON on node.js without jQuery

后端 未结 18 1993
醉话见心
醉话见心 2020-12-07 17:17

I have multiple JSON like those

var object1 = {name: \"John\"};
var object2 = {location: \"San Jose\"};

They are not nesting o

相关标签:
18条回答
  • 2020-12-07 17:54

    You can do it inline, without changing any variables like this:

    let obj1 = { name: 'John' };
    let obj2 = { surname: 'Smith' };
    let obj = Object.assign({}, obj1, obj2); // { name: 'John', surname: 'Smith' }
    
    0 讨论(0)
  • 2020-12-07 17:58

    Use spread operator. It is supported in Node since version 8.6

    const object1 = {name: "John"};
    const object2 = {location: "San Jose"};
    
    const obj = {...object1, ...object2}
    
    console.log(obj)
    //   {
    //     "name": "John",
    //     "location": "San Jose"
    //   }

    0 讨论(0)
  • 2020-12-07 17:58

    The below code will help you to merge two JSON object which has nested objects.

    function mergeJSON(source1,source2){
        /*
         * Properties from the Souce1 object will be copied to Source2 Object.
         * Note: This method will return a new merged object, Source1 and Source2 original values will not be replaced.
         * */
        var mergedJSON = Object.create(source2);// Copying Source2 to a new Object
    
        for (var attrname in source1) {
            if(mergedJSON.hasOwnProperty(attrname)) {
              if ( source1[attrname]!=null && source1[attrname].constructor==Object ) {
                  /*
                   * Recursive call if the property is an object,
                   * Iterate the object and set all properties of the inner object.
                  */
                  mergedJSON[attrname] = zrd3.utils.mergeJSON(source1[attrname], mergedJSON[attrname]);
              } 
    
            } else {//else copy the property from source1
                mergedJSON[attrname] = source1[attrname];
    
            }
          }
    
          return mergedJSON;
    }
    
    0 讨论(0)
  • 2020-12-07 18:01

    You can use Lodash

    const _ = require('lodash');
    
    let firstObject = {'email' : 'email@email.com};
    let secondObject = { 'name' : { 'first':message.firstName } };
    _.merge(firstObject, secondObject)
    
    0 讨论(0)
  • 2020-12-07 18:02

    There is an easy way of doing it in Node.js

    var object1 = {name: "John"};
    var object2 = {location: "San Jose"};
    

    To combine/extend this we can use ... operator in ECMA6

    var object1 = {name: "John"};
    var object2 = {location: "San Jose"};
    
    var result = {
      ...object1,
      ...object2
    }
    
    console.log(result)

    0 讨论(0)
  • 2020-12-07 18:03

    Underscore's extend is the easiest and quickest way to achieve this, like James commented.

    Here's an example using underscore:

    var _ = require('underscore'), // npm install underscore to install
      object1 = {name: "John"},
      object2 = {location: "San Jose"};
    
    var target = _.extend(object1, object2);
    

    object 1 will get the properties of object2 and be returned and assigned to target. You could do it like this as well, depending on whether you mind object1 being modified:

    var target = {};
    _.extend(target, object1, object2);
    
    0 讨论(0)
提交回复
热议问题