2

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.

4 Answers 4

2

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)
Sign up to request clarification or add additional context in comments.

2 Comments

@Medet Tleukabiluly How about my solution.
@Curious86 If you found that useful. Could you kindly upvote it please?
1

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);

Comments

1

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))

1 Comment

I think you shouldn't mix es6 here
0

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

3 Comments

What if in the future there gonna be a new key_z? That's not good solution
It was a hardcoded question. There's pros and cons to every solution. Whatever you think is the ideal solution could almost certainly be called overkill depending on the context. There's not enough context in the question to give a proper overall solution, but this definitely works and doesn't require extra code or libraries for a case that is unknown of if it even exists...
How about my solution?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.