I am trying to merge 3 different json array objects as a single object based on key values in the base json object in JavaScript. I haven’t tried anything like this before.
Data looks something like this.
var baseObj = [
{ "miId": 1, "val": 2, "smiList": [ { "smiId": '1a', "val": 3 }, { "smiId": '1b', "val": 4 } ] },
{ "miId": 2, "val": 22, "smiList": [ { "smiId": '2a', "val": 33 }, { "smiId": '2b', "val": 43 } ] }
]
var obj1 = [
{ "miId": 1, "val": 23, "smiList": [ { "smiId": '1a', "val": 13 }, { "smiId": '1c', "val": 14 } ] },
{ "miId": 4, "val": 24, "smiList": [ { "smiId": '2a', "val": 33 }, { "smiId": '2b', "val": 43 } ] }
]
var obj2 = [
{ "miId": 11, "val": 22, "smiList": [ { "smiId": '1a', "val": 53 }, { "smiId": '1c', "val": 14 } ] },
{ "miId": 2, "val": 43, "smiList": [ { "smiId": '2a', "val": 6 }, { "smiId": '2b', "val": 7 } ] }
]
My result has to be based on the base json and its key values. its basically like a left of obj1 and obj2 with baseobj array. I need to get all the objects of baseObj along with matched values of obj1 and obj2 based on their keys.
The result has to be:
var resultObj = [
{ "miId": 1, "val": 2, "obj1Val" :23, "smiList": [ {"smiId": '1a', "val": 3, "obj1Val": 13}, { "smiId": '1b', "val": 4 } ] },
{ "miId": 2, "val": 22, "obj2Val" :43, "smiList": [ { "smiId": '2a', "val": 33, "obj2Val" :6}, { "smiId": '2b', "val": 43, "obj2Val" :7, } ] }
]
can anyone please help me in how to achieve the above result. Thanks in advance.