I've been attempting to combine two JSON arrays with objects as elements. You can check out this plunkr file for both JSON files. I have successfully retrieved the expected final outcome array id, but I'm unsure how to structure the expected JSON as shown below. I am using underscore js for this task.
Note: If an object exists in newJson and not in currentJson, after merging, it will be in the 'inactive' state by default.
I'm not certain if I'm taking the correct approach. This is what I've tried:
var newJsonID = _.pluck(newJson, 'id');
var currentJsonID = _.pluck(currentJson, 'id');
var union = _.union(newJsonID, currentJsonID);
var intersection = _.intersection(currentJsonID, newJsonID);
var final = _.difference(union, _.difference( currentJsonID, intersection);
Expected Final Outcome:
[
{
"id": "12",
"property1Name": "1"
"status": "inactive"
},
{
"id": "11",
"property1Name": "1"
"status": "inactive"
},
{
"id": "10",
"property1Name": "1"
"status": "inactive"
},
{
"id": "9",
"property1Name": "1"
"status": "active"
}
]