Exclude some properties in comparison using isEqual() of lodash

邮差的信 提交于 2019-12-12 08:19:54

问题


I am using _.isEqual that compares 2 array of objects (ex:10 properties each object), and it is working fine.

Now there are 2 properties (creation and deletion) that i need not to be a part of comparison.

Example:

var obj1 = {name: "James", age: 17, creation: "13-02-2016", deletion: "13-04-2016"}
var obj2 = {name: "Maria", age: 17, creation: "13-02-2016", deletion: "13-04-2016"}

// lodash method...
_.isEqual(firstArray, secondArray)

回答1:


You can use omit() to remove specific properties in an object.

var result = _.isEqual(
  _.omit(obj1, ['creation', 'deletion']),
  _.omit(obj2, ['creation', 'deletion'])
);

var obj1 = {
  name: "James",
  age: 17,
  creation: "13-02-2016",
  deletion: "13-04-2016"
};

var obj2 = {
  name: "Maria",
  age: 17,
  creation: "13-02-2016",
  deletion: "13-04-2016"
};

var result = _.isEqual(
  _.omit(obj1, ['creation', 'deletion']),
  _.omit(obj2, ['creation', 'deletion'])
);

console.log(result);
<script src="https://cdn.jsdelivr.net/lodash/4.13.1/lodash.min.js"></script>



回答2:


@ryeballar's answer is not great for large objects because you are creating a deep copy of each object every time you do the comparison.

It's better to use isEqualWith. For example, to ignore differences in the "creation" and "deletion" properties:

var result = _.isEqualWith(obj1, obj2, (value1, value2, key) => {
    return key === "creation" || key === "deletion" ? true : undefined;
});

EDIT (important caveat pointed out in the comments): if objects have different numbers of keys, then isEqualWith considers them to be different, regadless of what your customizer does. Therefore do not use this approach if you want to ignore an optional property. Instead, consider using _.isMatch(), _.isMatchWith(), or @ryeballar's _.omit() approach.

Note that if you're writing for ES5 and earlier, you'll have to replace the arrow syntax (() => {) with function syntax (function() {)




回答3:


_.omit creates deep copy of the object. If you need to exclude only root props it is better to create shallow copy using, for example, destructuring assignment:

const x = { a: 4, b: [1, 2], c: 'foo' }
const y = { a: 4, b: [1, 2], c: 'bar' }

const { c: xC, ...xWithoutC } = x
const { c: yC, ...yWithoutC } = y

_.isEqual(xWithoutC, yWithoutC) // true
xWithoutC.b === x.b             // true, would be false if you use _.omit

Best way is not to create copies at all (TypeScript):

function deepEqual(
  x?: object | null,
  y?: object | null,
  ignoreRootProps?: Set<string>
) {
  if (x == null || y == null) return x === y

  const keys = Object.keys(x)
  if (!_.isEqual(keys, Object.keys(y)) return false

  for (let key of keys) {
    if (ignoreRootProps && ignoreRootProps.has(key)) continue
    if (!_.isEqual(x[key], y[key])) return false
  }
  return true
}



回答4:


You could map your array into a "cleaned" array, then compare those.

// Create a function, to do some cleaning of the objects.
var clean = function(obj) {
    return {name: obj.name, age: obj.age};
};

// Create two new arrays, which are mapped, 'cleaned' copies of the original arrays.
var array1 = firstArray.map(clean);
var array2 = secondArray.map(clean);

// Compare the new arrays.
_.isEqual(array1, array2);

This has the downside that the clean function will need to be updated if the objects are expecting any new properties. It is possible to edit it so that it removes the two unwanted properties instead.




回答5:


I see two options.

1) Make a second copy of each object that doesn't contain the creation or date.

2) Loop through all the properties and, assuming you know for certain that they both have the same properties, try something like this.

var x ={}
var y ={}
for (var property in x) {
if(property!="creation" || property!="deletion"){
if (x.hasOwnProperty(property)) {
        compare(x[property], y[property])
    }
}
}

Where compare() is some simple string or object comparison. If you are certain of the properties on one or both the objects, you can simplify this code a bit further, but this should work in most cases.



来源:https://stackoverflow.com/questions/37922967/exclude-some-properties-in-comparison-using-isequal-of-lodash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!