Ravi Agrawal
Ravi Agrawal

Reputation: 37

Add nested json object item to another json item

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?

Upvotes: 0

Views: 7293

Answers (5)

Saurabh Yadav
Saurabh Yadav

Reputation: 3386

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);

Upvotes: 0

Mark
Mark

Reputation: 92440

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)

Upvotes: 1

Roberto Zvjerković
Roberto Zvjerković

Reputation: 10127

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..

Upvotes: 0

Jack Bashford
Jack Bashford

Reputation: 44087

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.

Upvotes: 0

Nithya Rajan
Nithya Rajan

Reputation: 4884

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);

Upvotes: 0

Related Questions