【问题标题】:lodash: compare two objectslodash:比较两个对象
【发布时间】:2016-06-23 21:55:16
【问题描述】:

我想通过 lodash 制作这个:

first = { one: 1, two: 2, three: 3 }

second = { three: 3, one: 1 }

_.CustomisEqual(first, second);
// → true

third = { one: 1, two: 2, three: 3 }

fourth = { three: 4, one: 1 }

_.CustomisEqual(third, fourth);
// → false

但是通常_.isEqual不支持这种比较方法,有没有办法通过lodash进行这种比较?

【问题讨论】:

  • 您是在问如何编写这样的函数吗?你试过什么?你检查过isMatch 函数吗?
  • 是的,isMatch 为我工作,谢谢
  • _.isMatch() 在“第二个”变量是第一个参数并且“第一个”变量是第二个参数时不起作用。

标签: javascript underscore.js lodash


【解决方案1】:

function CustomisEqual(objOne, objTwo) {
  return !!_([objOne]).filter(objTwo).size();
}

first = { one: 1, two: 2, three: 3 }
second = { three: 3, one: 1 }

var resOne = CustomisEqual(first, second);

console.log('resOne ', resOne);
// → true

third = { one: 1, two: 2, three: 3 }
fourth = { three: 4, one: 1 }

var resTwo = CustomisEqual(third, fourth);

console.log('resTwo ', resTwo);
// → false
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.js"></script>

【讨论】:

    【解决方案2】:

    基本上是这样的:

    // mock lodash
    var _ = {}
    
    // create custom lodash method
    _.CustomisEqual = function (a, b) {
      
      // get array of the keys in object
      var aKeys = Object.keys(a) 
      var bKeys = Object.keys(b)
      // get the array of the keys with the smallest length
      var minKey = (aKeys.length > bKeys.length ? bKeys : aKeys)
      // get the obejct with the smallest and greatest length
      var minObj = (aKeys.length > bKeys.length ? b : a)
      var maxObj = (a === minObj ? b : a)
      // get the length from the smallest length array of the keys
      var minLen = Math.min(aKeys.length, bKeys.length)
      
      // iterate through the smallest object
      for (var i = 0; i < minLen; i++) {
        var currentKey = minKey[i]
        // ignore values that are in one but not the other
        if (maxObj[currentKey] !== undefined && 
            // compare defined values of both objects
            maxObj[currentKey] !== minObj[currentKey]) {
          // return false if they don't match
          return false
        }
      }
      
      // otherwise, they do match, so return true
      return true
    }
    
    var first = { one: 1, two: 2, three: 3 }
    var second = { three: 3, one: 1 }
    
    console.log(_.CustomisEqual(first, second)) // → true
    
    var third = { one: 1, two: 2, three: 4 }
    var fourth = { three: 3, one: 1 }
    
    console.log(_.CustomisEqual(third, fourth)) // → false

    希望对你有帮助!

    【讨论】:

      【解决方案3】:

      我认为以下解决方案可能有用。

      var first = { one: 1, two: 2, three: 3 };
      var second = { three: 3, one: 1 };
      var third = { one: 1, two: 2, three: 3 };
      var fourth = { three: 4, one: 1 };
      
      function customIsEquals(first,second) {
         var ret = true;
         _.forEach(second,function(value, key){
            if(first[key]!==value){
               ret = false;
            }
         });  
         return ret;
      }
      
      _.mixin({ 'customIsEquals': customIsEquals });
      
      console.log(_.customIsEquals(first,second));
      console.log(_.customIsEquals(third,fourth));
      &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js"&gt;&lt;/script&gt;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-05
        • 1970-01-01
        • 2019-08-02
        • 2014-01-26
        相关资源
        最近更新 更多