In my current project, I am using lodash to compare two arrays of objects and extract the differences between them. The goal is to add this difference to the original data set while maintaining the existing data intact. For instance, if there are initially 3 objects in orgData
, fetching newData
would result in having the same objects from orgData
plus an additional object.
var orgData = [{
"id": 1000,
"title": "First item"
}, {
"id": 1001,
"title": "Second item"
}];
var newData = [{
"id": 1000,
"title": "First item"
}, {
"id": 1001,
"title": "Second item"
}, {
"id": 1002,
"title": "Third item"
}];
The unique identifier for comparison that I am using is the id
. However, when attempting to execute my code, I encounter an error stating 'Cannot read property of 'id' undefined', which is understandable given the context.
_.filter(orgData, function(o, x) {
return o.id !== newData[x].id;
}).forEach(function(x) {
orgData.push(x);
});