Exclude some properties in comparison using isEqual() of lodash

Marco Santos picture Marco Santos · Jun 20, 2016 · Viewed 22.9k times · Source

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)

Answer

ryeballar picture ryeballar · Jun 21, 2016

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>