0

I want to add two nested objects in JSON in typescript.

In JSON given below I want to add second JSON's activityLogs item in first JSON's activityLogs.

JSON1:

[{"vehicleno":"SV028","devicE_CODE":"8505","activityLogs":
    [{"gpsdate":"01/03/2019","gpstime":"13:40:18"},
     {"gpsdate":"01/03/2019","gpstime":"13:38:18"},
     {"gpsdate":"01/03/2019","gpstime":"13:37:18"}]
}]

JSON2:

[{"vehicleno":"SV028","devicE_CODE":"8505","activityLogs":
    [{"gpsdate":"01/03/2019","gpstime":"13:46:18"},
    {"gpsdate":"01/03/2019","gpstime":"13:43:18"}]
}]

Result:

[{"vehicleno":"SV028","devicE_CODE":"8505","activityLogs":
    [{"gpsdate":"01/03/2019","gpstime":"13:46:18"},
    {"gpsdate":"01/03/2019","gpstime":"13:43:18"},
     {"gpsdate":"01/03/2019","gpstime":"13:40:18"},
    {"gpsdate":"01/03/2019","gpstime":"13:38:18"},
    {"gpsdate":"01/03/2019","gpstime":"13:37:18"}]
}]

How I can do this?

Ravi Agrawal
  • 27
  • 1
  • 9
  • Possible duplicate of [Native way to merge objects in Javascript](https://stackoverflow.com/questions/4740806/native-way-to-merge-objects-in-javascript) – Mike Doe Mar 01 '19 at 07:13
  • try to use `concat` with specific condition ,if not you can use lodash use merge.a[0].activityLogs = a[0].activityLogs.concat(b[0].activityLogs) – Dilip Tirumala Mar 01 '19 at 07:27

5 Answers5

1

You can use push() with the spread operator or concat and reassign:

var JSON1 = [{"vehicleno":"SV028","devicE_CODE":"8505","activityLogs":[{"gpsdate":"01/03/2019","gpstime":"13:40:18"},{"gpsdate":"01/03/2019","gpstime":"13:38:18"},{"gpsdate":"01/03/2019","gpstime":"13:37:18"}]}]
var JSON2 = [{"vehicleno":"SV028","devicE_CODE":"8505","activityLogs":[{"gpsdate":"01/03/2019","gpstime":"13:46:18"},{"gpsdate":"01/03/2019","gpstime":"13:43:18"}]}]

JSON1[0].activityLogs.push(...JSON2[0].activityLogs)

console.log(JSON1)

This assumes that your json arrays contain just the one top-level object. If that's not the case you need to add more details about how the two arrays are synchronized (for example will vehicleno be the same in both?).

As an example, if the vehicleno is a unique identifier in both arrays you could create a lookup of the JSON1 values and the use that to push into the appropriate arrays. This will update JSON1 in place even if it contains multiple vehicles:

var JSON1 = [{"vehicleno":"SV028","devicE_CODE":"8505","activityLogs":[{"gpsdate":"01/03/2019","gpstime":"13:40:18"},{"gpsdate":"01/03/2019","gpstime":"13:38:18"},{"gpsdate":"01/03/2019","gpstime":"13:37:18"}]}]
var JSON2 = [{"vehicleno":"SV028","devicE_CODE":"8505","activityLogs":[{"gpsdate":"01/03/2019","gpstime":"13:46:18"},{"gpsdate":"01/03/2019","gpstime":"13:43:18"}]}]

let lookup = JSON1.reduce((lookup, obj) => {
  lookup[obj.vehicleno] = obj
  return lookup
}, {})

JSON2.forEach(obj => lookup[obj.vehicleno].activityLogs.push(...obj.activityLogs))
console.log(JSON1)
Mark
  • 90,562
  • 7
  • 108
  • 148
0

You can use concatination array method.

let json1 = [{"vehicleno":"SV028","devicE_CODE":"8505","activityLogs":[{"gpsdate":"01/03/2019","gpstime":"13:40:18"},{"gpsdate":"01/03/2019","gpstime":"13:38:18"},{"gpsdate":"01/03/2019","gpstime":"13:37:18"}]}];

let json2 = [{"vehicleno":"SV028","devicE_CODE":"8505","activityLogs":[{"gpsdate":"01/03/2019","gpstime":"13:46:18"},{"gpsdate":"01/03/2019","gpstime":"13:43:18"}]}]


let result = json1[0].activityLogs.concat(json2[0].activityLogs);

console.log(result);
Nithya Rajan
  • 4,722
  • 19
  • 30
0

The simplest way is to concat the activityLogs:

var arr1 = [{
  "vehicleno": "SV028",
  "devicE_CODE": "8505",
  "activityLogs": [{
      "gpsdate": "01/03/2019",
      "gpstime": "13:40:18"
    },
    {
      "gpsdate": "01/03/2019",
      "gpstime": "13:38:18"
    },
    {
      "gpsdate": "01/03/2019",
      "gpstime": "13:37:18"
    }
  ]
}];
var arr2 = [{
  "vehicleno": "SV028",
  "devicE_CODE": "8505",
  "activityLogs": [{
      "gpsdate": "01/03/2019",
      "gpstime": "13:46:18"
    },
    {
      "gpsdate": "01/03/2019",
      "gpstime": "13:43:18"
    }
  ]
}];
var arr3 = arr1[0].activityLogs.concat(arr2[0].activityLogs);
console.log(arr3);
.as-console-wrapper {
  max-height: 100% !important;
  top: auto;
}

Note this will only work if you only have one object in the top-level array.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
0
result = json1;
/// result = Object.assign({}, json1); if you don't want to mutate the original json1
result.forEach(elem1 => elem1.activityLogs
    .concat(json2.find(elem2 => elem2.vehicleno === elem1.vehicleno).activityLogs));

Concat the activityLogs of the second array item to the first array item by finding the matching element by vehicleno..

Roberto Zvjerković
  • 9,657
  • 4
  • 26
  • 47
0
var json1 = [{"vehicleno":"SV028","devicE_CODE":"8505","activityLogs":
    [{"gpsdate":"01/03/2019","gpstime":"13:40:18"},
     {"gpsdate":"01/03/2019","gpstime":"13:38:18"},
     {"gpsdate":"01/03/2019","gpstime":"13:37:18"}]
},{"vehicleno":"SV02","devicE_CODE":"8505","activityLogs":
    [{"gpsdate":"01/03/2019","gpstime":"13:40:18"},
     {"gpsdate":"01/03/2019","gpstime":"13:38:18"},
     {"gpsdate":"01/03/2019","gpstime":"13:37:18"}]
}]


var json2 = [{"vehicleno":"SV028","devicE_CODE":"8505","activityLogs":
    [{"gpsdate":"01/03/2019","gpstime":"13:46:18"},
    {"gpsdate":"01/03/2019","gpstime":"13:43:18"}]
}];

var jsonCont = json1.concat(json2);

var result = Object.values(jsonCont.reduce((acc, o)=>{
    if(!acc.hasOwnProperty(o['vehicleno'])) {
        acc[o['vehicleno']] = Object.assign({}, o);
    } else {
        acc[o['vehicleno']]['activityLogs'] = acc[o['vehicleno']]['activityLogs'].concat(o['activityLogs']);
    }
    return acc;
}, {}));

console.log(result);
Saurabh Yadav
  • 3,303
  • 1
  • 10
  • 20