Curious86
Curious86

Reputation: 59

Javascript/underscore/lodash : Comparing object in array of objects and remove the matched object

I have on array of object, which looks like the following :

var arrayOfObjects = [
  {
    key_a : "value1",
    key_b : "value2"
  },
  {
    key_c : "value12",
    key_d : "value23"
  }
];

Now, I have another object :

 var objToCompare = {
    key_a : "value1",
    key_b : "value2"
 };

What are the possible ways to compare 'objToCompare' with 'arrayOfObjects' and remove the matching objects from 'arrayOfObjects' -- using javascript or underscore js or lodash?

So, the resultant array of object would look like :

var arrayOfObjects = [
  {
    key_c : "value12",
    key_d : "value23"
  }
];

*The object should only be removed from the array of objects, when all the properties are matched.

Upvotes: 2

Views: 280

Answers (4)

Toan Tran
Toan Tran

Reputation: 2047

I have found an easy way to solve your problem. Convert objectToCompare to array and using _.differenceWith() to compare.

Check my code below, please.

    var _ = require('lodash')

    var arrayOfObjects = [
      {
        key_a : "value1",
        key_b : "value2"
      },
      {
        key_c : "value12",
        key_d : "value23"
      }
    ]


    var objToCompare = {
       key_a : "value1",
       key_b : "value2"
    }

    var arrayToCompare = []
    arrayToCompare.push(objToCompare)

    var result = _.differenceWith(arrayOfObjects, arrayToCompare, _.isEqual)

    console.log(result)

Upvotes: 2

yanarchy
yanarchy

Reputation: 11

You can just use the _.reject and _.isEqual method. This is almost the exactly opposite of the _.where method in Underscore/Lodash.

let result = _.reject(arrayOfObjects, (obj) => _.isEqual(obj, objToCompare))

Upvotes: 1

Jack
Jack

Reputation: 21163

Should be as simple as:

arrayOfObjects.filter((item) => !(item.key_a === objToCompare.key_a && item.key_b === objToCompare.key_b))

Edit: FWIW: This is a pure javascript solution that only does what it needs to and doesn't effectively recreate the lodash/underscore isEqual

Upvotes: 0

Medet Tleukabiluly
Medet Tleukabiluly

Reputation: 11930

Pure javascript version

var arrayOfObjects = [{
    key_a: "value1",
    key_b: "value2"
  },
  {
    key_c: "value12",
    key_d: "value23"
  }
];

var objToCompare = {
  key_a: "value1",
  key_b: "value2"
};

// credits https://stackoverflow.com/a/6713782/2308005
function objectEquals(x, y) {
  if (x === y) return true;
  if (!(x instanceof Object) || !(y instanceof Object)) return false;
  if (x.constructor !== y.constructor) return false;
  for (var p in x) {
    if (!x.hasOwnProperty(p)) continue;
    if (!y.hasOwnProperty(p)) return false;
    if (x[p] === y[p]) continue;
    if (typeof(x[p]) !== "object") return false;
    if (!Object.equals(x[p], y[p])) return false;
  }
  for (p in y) {
    if (y.hasOwnProperty(p) && !x.hasOwnProperty(p)) return false;
  }
  return true;
}


var result = arrayOfObjects.filter(function(item) {
  return !objectEquals(item, objToCompare);
});

console.log(result);

Upvotes: 1

Related Questions