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

Javascript compare objects having functions using lodash isEqual - Stack Overflow

programmeradmin4浏览0评论

how to compare two objects for equality if they have functions? lodash's isEqual works really well until functions are thrown in:

_.isEqual({
    a: 1,
    b: 2
}, {
    b: 2,    
    a: 1
});

// -> true

_.isEqual({
    a: 1,
    b: 2,
    c: function () {
        return 1;
    }
}, {
    a: 1,    
    b: 2,
    c: function () {
          return 1;
    }
});

// -> false

how to compare two objects for equality if they have functions? lodash's isEqual works really well until functions are thrown in:

_.isEqual({
    a: 1,
    b: 2
}, {
    b: 2,    
    a: 1
});

// -> true

_.isEqual({
    a: 1,
    b: 2,
    c: function () {
        return 1;
    }
}, {
    a: 1,    
    b: 2,
    c: function () {
          return 1;
    }
});

// -> false
Share Improve this question edited May 13, 2015 at 10:43 Chris Pietschmann 29.9k36 gold badges123 silver badges167 bronze badges asked May 13, 2015 at 10:35 pistacchiopistacchio 58.9k110 gold badges287 silver badges432 bronze badges 3
  • two anonymous function have two different prototypes, so two different objects! – user757095 Commented May 13, 2015 at 10:37
  • It really depends on what you are going to compare. Best solution would be to create a hash unique per object and compare it, or, knowing the structure, make an equal function for that particular object. – Jack Commented May 13, 2015 at 10:40
  • Even though the functions do the same, they will never be equal. Do you want to ignore the functions and compare? – thefourtheye Commented May 13, 2015 at 10:54
Add a comment  | 

4 Answers 4

Reset to default 7

Are you sure you want to compare functions? If you only care about comparing every property that isn't a function, this is easy to do with lodash:

var o1 = { a: 1, b: 2, c: function() { return 1; } },
    o2 = { a: 1, b: 2, c: function() { return 1; } };

_.isEqual(o1, o2)
// → false

_.isEqual(_.omit(o1, _.functions(o1)), _.omit(o2, _.functions(o2)));
// → true

The functions() function returns a list of function properties, and using omit(), you can get rid of them.

This is what I tried:

_.isEqual(o1, o2, function(val1, val2) {
  if(_.isFunction(val1) && _.isFunction(val2)) {
    return val1.toString() === val2.toString();
  }
})

Lodash supports a customizer function which allows you to write your own equality checks. This seems to be a good enough test to see if the functions are character by character the same.

Try isEqualWith instead:

import { isEqualWith, isFunction } from 'lodash-es'

const o1 = { fn() {} }

const o2 = { fn() {} }

const equal = isEqualWith(o1, o2, (v1, v2) =>
  // if `customizer` returns `undefined`, comparisons are handled by the method instead
  isFunction(v1) && isFunction(v2) ? `${v1}` === `${v2}` : undefined,
)

console.log({ equal }) // { equal: true }

As the lodash documentation states:

Functions and DOM nodes are not supported.

https://lodash.com/docs#isEqual

发布评论

评论列表(0)

  1. 暂无评论