compare-anything
Version:
Compares objects and arrays and tells you which props or values are duplicates, and which are only present once.
22 lines (21 loc) • 724 B
JavaScript
import { pick } from 'filter-anything';
export function compareObjectsBasedOn(propKeys, objects) {
const baseProps = propKeys;
const baseObject = objects[0];
const differentPropsSet = objects.reduce((carry, object, index) => {
if (index === 0)
return carry;
for (const prop of baseProps) {
if (object[prop] !== baseObject[prop])
carry.add(prop);
}
return carry;
}, new Set());
const differentProps = [...differentPropsSet];
const differentPropsPicked = objects.map((object) => pick(object, differentProps));
return {
differentProps,
differentPropsPicked,
equal: differentProps.length === 0,
};
}