最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Find difference in Objects using underscore.js - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share asked Jul 29, 2016 at 18:51 0_00_0 5371 gold badge7 silver badges21 bronze badges 4
  • 1 There's no such thing as a "JSON Object" – Andreas Commented Jul 29, 2016 at 18:53
  • Isn't difference for an array? – epascarello 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./flitbit/diff (Well, that's a bad example, but the libraries you seek exist.) – Dave Newton Commented Jul 29, 2016 at 18:56
  • @DaveNewton Thanks. This is what I was looking for. – 0_0 Commented Jul 29, 2016 at 19:01
Add a ment  | 

3 Answers 3

Reset to default 5
/**
 * Deep diff between two object, using underscore
 * @param  {Object} object Object pared
 * @param  {Object} base   Object to pare 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);
}

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

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./ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

发布评论

评论列表(0)

  1. 暂无评论