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

查看:26
本文介绍了如何为 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 Set 对象没有任何比较方法或自定义比较可扩展性.

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 这篇文章和讨论的一些信息特点:

Here's some info from this article and discussion of ES6 features:

5.2 为什么我不能配置 map 和 set 如何比较键和值?

问题:如果有办法配置什么地图就好了键和什么集合元素被认为是相等的.为什么没有?

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()).然而,这种方法是可变对象的问题:一般来说,如果一个对象发生变化,它的集合中的位置"也必须改变.但这不是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天全站免登陆