我们知道Set是JS的一个种新的数据结构,和数组类似,和数组不同的是它可以去重,比如存入两个1或两个"123",只有1条数据会存入成功,但有个特殊情况,如果添加到set的值是引用类型,比如数组、对象,他将无法自动去重。因为值相同的两个引用类型地址是不一样的。下面来看一个例子:
var mySet = new Set()
mySet.add([-1,0,1])
mySet.add([-1,0,1])
mySet.add({a: 1})
mySet.add({a: 1})
mySet.size // 4
console.log(Array.from(mySet)) // [[-1, 0, 1], [-1, 0, 1], {a: 1}, {a: 1}]
我们来看看mdn上的文档描述:
Set - JavaScript | MDNThe Set object lets you store unique values of any type, whether primitive values or object references.https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
Description
Set objects are collections of values. You can iterate through the elements of a set in insertion order. A value in the Set may only occur once; it is unique in the Set's collection.
集合对象是值的集合。您可以按插入顺序遍历集合的元素。集合中的值只能出现一次;它在集合集合中是唯一的。
Value equality
Because each value in the Set has to be unique, the value equality will be checked. In an earlier version of ECMAScript specification, this was not based on the same algorithm as the one used in the === operator. Specifically, for Sets, +0 (which is strictly equal to -0) and -0 were different values. However, this was changed in the ECMAScript 2015 specification. See "Key equality for -0 and 0" in the browser compatibility table for details. NaN and undefined can also be stored in a Set. All NaN values are equated (i.e. NaN is considered the same as NaN, even though NaN !== NaN).
由于集合中的每个值都必须是唯一的,因此将检查值是否相等。在早期版本的ECMAScript规范中,这与==运算符中使用的算法不同。具体来说,对于集,+0(严格等于-0)和-0是不同的值。然而,这在ECMAScript 2015规范中有所更改。有关详细信息,请参阅浏览器兼容性表中的“-0和0的密钥相等”。NaN和undefined也可以存储在Set中。所有NaN值都相等(即,NaN被认为与NaN相同,即使NaN!==NaN)。
我们可以简单理解为,像Set实例add数据时,每次都会进行等值判断,类似于将add的元素与每个元素进行 === 比较。因此对引用类型的去重是无效的
var stra = "test"
var strb = "test"
var a = { a : 1}
var b = { a : 1}
stra === strb // true
a === b // false 尽管他们都是对象 { a: 1 },但他们存储的地址是不一样的
那这种情况怎么去重呢。我们可以自己写方法来处理,以数组为例子,可以将值[-1, 0, 1].join('|') 处理下,添加进去,到时统一再split出来