Array of object deep comparison with lodash

后端 未结 3 1200
悲哀的现实
悲哀的现实 2020-12-03 21:05

I\'ve 2 array of objects that I\'d deeply compare with lodash

However, I\'ve a prob with it:

> var x = [{a:1, b:2}, {c:3, d:4}];
>         


        
3条回答
  •  悲哀的现实
    2020-12-03 21:51

    You can make use of differenceWith() with an isEqual() comparator, and invoke isEmpty to check if they are equal or not.

    var isArrayEqual = function(x, y) {
      return _(x).differenceWith(y, _.isEqual).isEmpty();
    };
    
    var result1 = isArrayEqual(
      [{a:1, b:2}, {c:3, d:4}],
      [{b:2, a:1}, {d:4, c:3}]
    );
    
    var result2 = isArrayEqual(
      [{a:1, b:2, c: 1}, {c:3, d:4}],
      [{b:2, a:1}, {d:4, c:3}]
    );
    
    document.write([
      '
    ', '
    ', ].join(''));

    UPDATE June 22, 2018

    This update is in response to the comment below:

    @ryeballar if any of the array is undefined it returns true. How would you solve this. Thanks in advance buddy

    As stated in the differenceWith documentation:

    The order and references of result values are determined by the first array.

    This means that as long as all the items in the first array will match everything else in the second array, then the resulting array from the differenceWith invocation will be empty.

    An alternative solution that truly solves the problem is to use xorWith() with the same chain of functions from the solution above.

    var isArrayEqual = function(x, y) {
      return _(x).xorWith(y, _.isEqual).isEmpty();
    };
    
    var result1 = isArrayEqual(
      [{a:1, b:2}, {c:3, d:4}],
      [{b:2, a:1}, {d:4, c:3}]
    );
    
    var result2 = isArrayEqual(
      [{a:1, b:2, c: 1}, {c:3, d:4}],
      [{b:2, a:1}, {d:4, c:3}]
    );
    
    var result3 = isArrayEqual(
      [{a:1, b:2, c: 1}, {c:3, d:4}],
      [{b:2, a:1}, {d:4, c:3}, undefined]
    );
    
    console.log('result1:', result1);
    console.log('result2:', result2);
    console.log('result3:', result3);
    .as-console-wrapper{min-height:100%;top:0}

提交回复
热议问题