代码之家  ›  专栏  ›  技术社区  ›  Learning-Overthinker-Confused

使用深度比较或json比较两个对象。字符串化?

  •  5
  • Learning-Overthinker-Confused  · 技术社区  · 7 年前

    $scope.new = [
      {
        "name": "Node-1",
        "isParent": true,
        "text" : [
           {
               "str" : "This is my first Node-1 string",
               "parent":[]
           },
           {
              "str" : "This is my second Node-1 string",
               "parent":[]
           }],
         "nodes": [
          {
            "name": "Node-1-1",
            "isParent": false,
             "text" : [
               {
                 "str" : "This is my first Node-1-1 string",
                 "parent":[]
               },
               {
                 "str" : "This is my second Node-1-1 string",
                 "parent":[]
               }],
               "nodes": [
               {
                "name": "Node-1-1-1",
                "isParent": false,
                "text" : [
                {
                  "str" : "This is my first Node-1-1-1 string",
                  "parent":[]
                },
                {
                  "str" : "This is my second Node-1-1-1 string",
                  "parent":[]
                }],
                "nodes": []
              }
            ]
          }
        ]
      }
    ]
    

    angular.equal 这将在比较2对象时忽略该属性。

     console.log(angular.equals($scope.new,$scope.copy)); 
    

    所以,在做研究时,我得到了以下答案,即使用lodash具有发射选项,但问题是我想省略了创建副本,我想在使用lodash的情况下,我的性能会下降。

    Exclude some properties in comparison using isEqual() of lodash

    类似这样:

    var str1 = JSON.stringify(JSON.stringify($scope.new));
    
    var str2 = JSON.stringify(JSON.stringify($scope.copy));
    
    console.log(str1==str2);
    

    注: isParent 比较2对象时的属性。

    比较2个对象的最佳方法是什么?

    2 回复  |  直到 7 年前
        1
  •  2
  •   quirimmo    7 年前

    在这些情况下,转换为字符串并不是最好的方法。 将其作为对象。

    const propertiesToExclude = ['isParent'];
    let result = _.isEqual(
      _.omit(obj1, propertiesToExclude),
      _.omit(obj2, propertiesToExclude)
    );
    

    使用普通AngularJS创建对象的副本,删除不需要的属性,然后比较它们:

    let firstObj = angular.copy(obj1);
    let secondObj = angular.copy(obj2);
    const propertiesToExclude = ['isParent'];
    function removeNotComparatedProperties(obj) {
      propertiesToExclude.forEach(prop => {
        delete obj[prop];
      });
    }
    
    removeNotComparatedProperties(firstObj);
    removeNotComparatedProperties(secondObj);
    angular.equals(firstObj, secondObj);
    
        2
  •  1
  •   TimoStaudinger    7 年前

    如果使用,则可以使用lodash并覆盖用于深度比较的标准比较器 _.isEqualWith

    var objA = {
      isParent: true,
      foo: {
        isParent: false,
        bar: "foobar"
      }
    };
    
    var objB = {
      isParent: false,
      foo: {
        isParent: true,
        bar: "foobar"
      }
    };
    
    var comparator = function(left, right, key) {
      if (key === 'isParent') return true; // if the key is 'isParent', mark the values equal
      else return undefined;               // else fall back to the default behavior
    }
    
    var isEqual = _.isEqualWith(objA, objB, comparator);
    
    console.log(isEqual); // true
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

    排除多个属性 ,相应地扩展比较器功能:

    var comparator = function(left, right, key) {
      if (key === 'isParent' || key === 'anotherKey') return true;
      else return undefined;
    }
    

    您还可以在语法上使用许多不同的方法,这取决于您喜欢的方法--switch语句、迭代的数组。。。