5

How to join two JSON Array objects in Node.

I want to join obj1 + obj2 so I can get the new JSON object:

obj1 = [ { t: 1, d: 'AAA', v: 'yes' },
         { t: 2, d: 'BBB', v: 'yes' }]

obj2 = [ { t: 3, d: 'CCC', v: 'yes' },
        { t: 4, d: 'DDD', v: 'yes' }]


output = [ { t: 1, d: 'AAA', v: 'yes' },
           { t: 2, d: 'BBB', v: 'yes' },
           { t: 3, d: 'CCC', v: 'yes' },
           { t: 4, d: 'DDD', v: 'yes' }]
3

7 Answers 7

10

var output = obj1.concat(obj2);

Sign up to request clarification or add additional context in comments.

6 Comments

@coriano35 You need to provide more information. What does "not working" mean? Are you given an error? Or does your var output look nothing like what you expected?
@coriano35 Great, we're a step closer! What is obj1? Where is it defined? Feel free to edit your original question to tell us how obj1 really gets its value.
the obj1 value come from a url data with a format like this: <DTYYYYMMDD>,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOL> 19840105,10.5435,10.6522,10.5435,10.6522,2300 19840106,10.6522,10.7609,10.6522,10.7609,36572 19840109,10.7609,10.7609,10.7609,10.7609,2992 19840110,10.8696,10.9783,10.8696,10.9783,127648 19840111,11.0869,11.0869,11.0869,11.0869,26912 19840112,11.0869,11.4131,11.0869,11.4131,21620
and parse by a function: var result = { d: [], t: [], c: [], v: [] }; var lines = data.split('\n'); for (var i = 1; i < lines.length - 1; i++) { var items = lines[i].split(","); result.d.push(items[0]); result.t.push(items[1]); result.c.push(parseFloat(items[2])); result.v.push(parseFloat(items[3])); }
and put it in a json object
|
8
obj1 = [ { t: 1, d: 'AAA', v: 'yes' },
         { t: 2, d: 'BBB', v: 'yes' }]

obj2 = [ { t: 3, d: 'CCC', v: 'yes' },
        { t: 4, d: 'DDD', v: 'yes' }]

var output = obj1.concat(obj2);

console.log(output);

Comments

6

try

  Object.assign(obj1, obj2);

For Details check Here

 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 }

Comments

2

It can be done easily using ES6,

const output = [...obj1, ...obj2];

1 Comment

This is the right answer. The others are pretty ridiculous in comparison!
0

i already got an answer from the link provided by Pravin

var merge = function() {
var destination = {},
    sources = [].slice.call( arguments, 0 );
sources.forEach(function( source ) {
    var prop;
    for ( prop in source ) {
        if ( prop in destination && Array.isArray( destination[ prop ] ) ) {

            // Concat Arrays
            destination[ prop ] = destination[ prop ].concat( source[ prop ] );

        } else if ( prop in destination && typeof destination[ prop ] === "object" ) {

            // Merge Objects
            destination[ prop ] = merge( destination[ prop ], source[ prop ] );

        } else {

            // Set new values
            destination[ prop ] = source[ prop ];

        }
    }
});
return destination;
};

console.log(JSON.stringify(merge({ a: { b: 1, c: 2 } }, { a: { b: 3, d: 4 } })));

Comments

-1

you can use jmerge package.

npm i jmerge
const jm = require('jmerge')

jm(obj1,obj2,obj3,...) //merging json data

Comments

-2

I simply convert the arrays to strings, join them crudely with a comma, and then parse the result to JSON:

newJson=JSON.parse(
    JSON.stringify(copyJsonObj).substring(0,JSON.stringify(copyJsonObj).length-1) +
    ',' + 
    JSON.stringify(jsonObj).substring(1)
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.