javascriptjson

Concatenate two JSON objects


I have two JSON objects with the same structure and I want to concat them together using Javascript. Is there an easy way to do this?


Solution

  • Based on your description in the comments, you'd simply do an array concat():

    var jsonArray1 = [{'name': "doug", 'id':5}, {'name': "dofug", 'id':23}];
    var jsonArray2 = [{'name': "goud", 'id':1}, {'name': "doaaug", 'id':52}];
    
    jsonArray1 = jsonArray1.concat(jsonArray2);
    
    console.log(jsonArray1)
    // [{'name': "doug", 'id':5}, {'name': "dofug", 'id':23}, {'name': "goud", 'id':1}, {'name': "doaaug", 'id':52}];