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?

2
  • Possible duplicate of Native way to merge objects in Javascript Commented Mar 1, 2019 at 7: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) Commented Mar 1, 2019 at 7:27

5 Answers 5

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)

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

2 Comments

Thanks @Mark, this is working for me. But it is pushing data in bottom but I want this in top.
Hi @Mark, I used unshift instead of push and it is adding on first index. It solve my problem
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);

1 Comment

@bear-nithi Its concat but removed the vehicleno and device_code.
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.

1 Comment

Its concat but removed the vehicleno and device_code
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..

Comments

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

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.