Simple problem here: 2 Json objects I would like to merge, while renaming the second.
First Array (obj1):
[
{
"name":"Metric 1",
"value":33731487,
},
{
"name":"Metric 2",
"value":11252893,
}
]
Second Array (obj2):
[
{
"name":"Metric 1",
"value":118181851,
},
{
"name":"Metric 2",
"value":15151,
}
]
Desired Result:
[
{
"name":"Metric 1", // Obj1
"value":118181851, // Obj1
"name_compare":"Metric 1", // Obj2
"value_compare":148748, // Obj2
},
{
"name":"Metric 2", // Obj1
"value":15151, // Obj1
"name_compare":"Metric 2", // Obj2
"value_compare":741178, // Obj2
}
]
So I tried:
Renaming the Obj2 (works ok):
function JsonRename(obj) {
var output = {};
for (i in obj) {
if (Object.prototype.toString.apply(obj[i]) === '[object Object]') {
output[i] = JsonRename(obj[i]);
} else {
output[i+'_compare'] = obj[i];
}
}
return output;
}
I've then tried to merge them by using:
function JsonMergeCompare(obj1, obj2 ) {
var renamed_obj2 = JsonRename(obj2);
var output = {};
for (i in obj1) {
output[i] = obj1[i];
output[i] = renamed_obj2[i];
}
return output;
}
My problem is most certainly in the function above, because it only returns the obj2 (which makes sense as I'm iterating over output[i] twice) but how can I get in and change only the key -> values?