Combine or merge JSON on node.js without jQuery

后端 未结 18 2063
醉话见心
醉话见心 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条回答
  •  旧时难觅i
    2020-12-07 17:49

    Lodash is a another powerful tool-belt option for these sorts of utilities. See: _.merge() (which is recursive)

    var object = {
      'a': [{ 'b': 2 }, { 'd': 4 }]
    };
    var other = {
      'a': [{ 'c': 3 }, { 'e': 5 }]
    }; 
    _.merge(object, other);
    // => { 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }] } 
    

提交回复
热议问题