1

So I have two JSON objects and I am trying to find difference between them using underscore js. However for some reason its returning me the whole object instead of just returning the difference. My goal here is to get the pattern back as its the only thing different.

var a = {
        "name":"donor",
        "label":"Donor Data File (donor)",
        "pattern":"^donor(\\.[a-zA-Z0-9]+)?\\.txt(?:\\.gz|\\.bz2)?$"
};
var b = {
         "name":"donor",
         "label":"Donor Data File (donor)",
         "pattern":"^donor(\\.[a-zA-Z0-9]+)?\\.txt(?:\\.gz)?$"
};

console.log(_.difference(a,b));

Am I not understanding the use case of _.difference properly? Heres a JSFiddle in case needed.

4
  • 1
    There's no such thing as a "JSON Object" Commented Jul 29, 2016 at 18:53
  • Isn't difference for an array? Commented Jul 29, 2016 at 18:55
  • 1
    _.difference is for arrays; it sounds more like you want a full-on object diffing solution. E.g., github.com/flitbit/diff (Well, that's a bad example, but the libraries you seek exist.) Commented Jul 29, 2016 at 18:56
  • @DaveNewton Thanks. This is what I was looking for. Commented Jul 29, 2016 at 19:01

3 Answers 3

5
/**
 * Deep diff between two object, using underscore
 * @param  {Object} object Object compared
 * @param  {Object} base   Object to compare with
 * @return {Object}        Return a new object who represent the diff
 */
const difference = (object, base) => {
  const changes = (object, base) => (
    _.pick(
      _.mapObject(object, (value, key) => (
        (!_.isEqual(value, base[key])) ?
          ((_.isObject(value) && _.isObject(base[key])) ? changes(value, base[key]) : value) :
          null
      )),
      (value) => (value !== null)
    )
  );
  return changes(object, base);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect solution !
1

_.difference is meant to compare arrays, you're comparing objects. See this answer: using underscore's “difference” method on arrays of objects

Comments

0

Underscore has method isMatch,but no method that will return difference for objects, that takes 2 as parameter Objects and match their properties

var stooge = {name: 'moe', age: 32};
_.isMatch(stooge, {age: 32});

you can create your own implementation

function getDiffProperties(object1,object2){
      var difference  = [];
  
      for(key in object1){
        if(object1[key] != object2[key]){
          difference.push(key)
        }
      }

      for(key in object2){
        if(object1[key] != object2[key]){
          difference.push(key)
        }
      }
     
     return difference
}

console.log(getDiffProperties({name: 'moe', age: 32},{age: 32}))
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

1 Comment

Match returns a boolean, though. OP is trying to get an object back that has the same shape as only the differing fields.

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.