Here is a snippet of working code that raises a question about refactoring to improve the readability and functionality. Consider renaming the method to isPropValueSame
.
import * as _ from 'lodash';
const diff = _.differenceWith(sourceList, comparatorList, this.isSame);
isSame = (objA: any, objB: any) => (_.has(objA, 'id') && _.has(objB, 'id') ? objA['id'] === objB['id'] : false);
The goal is to pass a property name string like this.isSame('id'))
;
objA
and objB
represent items from lists: sourceList
and comparatorList
, which could look something like:
const sourceList = [
{ id: 1, prop2: { prop21: someValue }, prop3: prop3Value },
{ id: 2, prop2: { prop21: someValue }, prop3: prop3Value },
{ id: 3, prop2: { prop21: someValue }, prop3: prop3Value },
];
const comparatorList = [
{ id: 1, prop2: { prop21: someValue }, prop3: prop3Value },
//{ id: 2, prop2: { prop21: someValue }, prop3: prop3Value },
{ id: 3, prop2: { prop21: someValue }, prop3: prop3Value },
];
In the given test case data (where the second item in comparatorList
is commented out), the output of the comparator function would be the item with an id
equal to 2
because it's not found during comparison by the isSame
delegate function.