如何为JavaScript Set自定义对象相等性 [英] How to customize object equality for JavaScript Set

查看:550
本文介绍了如何为JavaScript Set自定义对象相等性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新ES 6(Harmony)推出新的 Set 对象。 Set使用的身份算法类似于 === 运算符,因此不太适合比较对象:

New ES 6 (Harmony) introduces new Set object. Identity algorithm used by Set is similar to === operator and so not much suitable for comparing objects:

var set = new Set();
set.add({a:1});
set.add({a:1});
console.log([...set.values()]); // Array [ Object, Object ]

如何自定义Set对象的相等性以便做深对象比较呢?有什么像Java equals(Object)

How to customize equality for Set objects in order to do deep object comparison? Is there anything like Java equals(Object)?

推荐答案

ES6 设置对象没有任何比较方法或自定义比较扩展性。

The ES6 Set object does not have any compare methods or custom compare extensibility.

.has() .add() .delete()方法只能使用它作为基元的相同实际对象或相同值,并且没有办法插入或替换该逻辑。

The .has(), .add() and .delete() methods work only off it being the same actual object or same value for a primitive and don't have a means to plug into or replace just that logic.

您可以从 Set 中推导出自己的对象,并替换 .has( ) .add() .delete()包含某些内容的方法深度对象比较首先要查找该项是否已经在Set中,但性能可能不会很好,因为底层的 Set 对象根本不会有帮助。在调用原始 .add()之前,您可能只需要对所有现有对象进行强力迭代以使用您自己的自定义比较查找匹配。

You could presumably derive your own object from a Set and replace .has(), .add() and .delete() methods with something that did a deep object comparison first to find if the item is already in the Set, but the performance would likely not be good since the underlying Set object would not be helping at all. You'd probably have to just do a brute force iteration through all existing objects to find a match using your own custom compare before calling the original .add().

以下是来自这个的一些信息ES6功能的文章和讨论


5.2为什么我不能配置地图和集合比较键的方式和值?

问题:如果有一种方法可以配置映射
键以及哪些集合元素被认为是相等的,那就太好了。为什么不存在?

Question: It would be nice if there were a way to configure what map keys and what set elements are considered equal. Why isn’t there?

答案:该功能已被推迟,因为很难实现正确有效的
。一种选择是将回调交给指定相等的
集合。

Answer: That feature has been postponed, as it is difficult to implement properly and efficiently. One option is to hand callbacks to collections that specify equality.

Java中提供的另一个选项是通过方法
指定相等性对象实现(Java中的equals())。但是,对于可变对象,这种方法是
有问题:通常,如果对象发生更改,其集合中的
location也必须更改。但这不是
Java中发生的事情。 JavaScript可能会更安全地转换为
,只能按特殊不可变对象
(所谓的值对象)的值进行比较。按值比较意味着如果两个值
的内容相等,则它们被认为是相等的。原始值为
,按JavaScript值比较。

Another option, available in Java, is to specify equality via a method that object implement (equals() in Java). However, this approach is problematic for mutable objects: In general, if an object changes, its "location" inside a collection has to change, as well. But that’s not what happens in Java. JavaScript will probably go the safer route of only enabling comparison by value for special immutable objects (so-called value objects). Comparison by value means that two values are considered equal if their contents are equal. Primitive values are compared by value in JavaScript.

这篇关于如何为JavaScript Set自定义对象相等性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆