Combine or merge JSON on node.js without jQuery

后端 未结 18 1994
醉话见心
醉话见心 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 18:03

    I see that this thread is too old, but I put my answer here just in logging purposes.

    In one of the comments above you mentioned that you wanted to use 'express' in your project which has 'connect' library in the dependency list. Actually 'connect.utils' library contains a 'merge' method that does the trick. So you can use the 3rd party implementation without adding any new 3rd party libraries.

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

    You can also use this lightweight npm package called absorb

    It is 27 lines of code, 1kb and uses recursion to perform deep object merges.

    var absorb = require('absorb');
    var obj1, obj2;
    
    obj1 = { foo: 123, bar: 456 };
    obj2 = { bar: 123, key: 'value' }
    
    absorb(obj1, obj2);
    
    console.log(obj1); // Output: { foo: 123, bar: 123, key: 'value' }
    

    You can also use it to make a clone or only transfer values if they don't exist in the source object, how to do this is detailed in the link provided.

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

    You should use "Object.assign()"

    There's no need to reinvent the wheel for such a simple use case of shallow merging.

    The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.

    var o1 = { a: 1 };
    var o2 = { b: 2 };
    var o3 = { c: 3 };
    
    var obj = Object.assign(o1, o2, o3);
    console.log(obj); // { a: 1, b: 2, c: 3 }
    console.log(o1);  // { a: 1, b: 2, c: 3 }, target object itself is changed
    

    Even the folks from Node.js say so:

    _extend was never intended to be used outside of internal NodeJS modules. The community found and used it anyway. It is deprecated and should not be used in new code. JavaScript comes with very similar built-in functionality through Object.assign.


    Update:

    You could use the spread operator

    Since version 8.6, it's possible to natively use the spread operator in Node.js. Example below:

    let o1 = { a: 1 };
    let o2 = { b: 2 };
    let obj = { ...o1, ...o2 }; // { a: 1, b: 2 }
    

    Object.assign still works, though.


    PS1: If you are actually interested in deep merging (in which internal object data -- in any depth -- is recursively merged), you can use packages like deepmerge, assign-deep or lodash.merge, which are pretty small and simple to use.

    PS2: Keep in mind that Object.assign doesn't work with 0.X versions of Node.js. If you are working with one of those versions (you really shouldn't by now), you could use require("util")._extend as shown in the Node.js link above -- for more details, check tobymackenzie's answer to this same question.

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

    It can easy be done using Object.assign() method -

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

    now object3 is { name: 'John', location: 'San Jose' }

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

    Use merge.

    $ npm install merge
    

    Sample code:

    var merge = require('merge'), // npm install -g merge
        original, cloned;
    
    console.log(
    
        merge({ one: 'hello' }, { two: 'world' })
    
    ); // {"one": "hello", "two": "world"}
    
    original = { x: { y: 1 } };
    
    cloned = merge(true, original);
    
    cloned.x.y++;
    
    console.log(original.x.y, cloned.x.y); // 1, 2
    
    0 讨论(0)
  • 2020-12-07 18:09

    Let object1 and object2 be two JSON object.

    var object1 = [{"name": "John"}];
    var object2 = [{"location": "San Jose"}];
    
    object1.push(object2);
    

    This will simply append object2 in object1:

    [{"name":"John"},{"location":"San Jose"}]
    
    0 讨论(0)
提交回复
热议问题