From 49ec7d0998deb6ac154cfde4e48fcff9eb2bf7c9 Mon Sep 17 00:00:00 2001 From: Ruan Yifeng Date: Sat, 10 Jan 2015 08:27:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9set?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/set-map.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/docs/set-map.md b/docs/set-map.md index d243a7c..2ec638c 100644 --- a/docs/set-map.md +++ b/docs/set-map.md @@ -32,7 +32,7 @@ items.size ``` -向Set加入值的时候,不会发生类型转换,5和“5”是两个不同的值。Set内部判断两个值是否精确相等,使用的是精确相等运算符(===)。这意味着,两个对象总是不相等的。 +向Set加入值的时候,不会发生类型转换,所以5和“5”是两个不同的值。Set内部判断两个值是否不同,使用的算法类似于精确相等运算符(===),唯一的例外是NaN等于自身。这意味着,两个对象总是不相等的。 ```javascript @@ -235,6 +235,21 @@ set = new Set([...set].filter(x => (x % 2) == 0)); ``` +因此使用Set,可以很容易地实现并集(Union)和交集(Intersect)。 + +```javascript + +let a = new Set([1,2,3]); +let b = new Set([4,3,2]); + +let union = new Set([...a, ...b]); +// [1,2,3,4] + +let intersect = new Set([...a].filter(x => b.has(x))); +// [2,3] + +``` + ## WeakSet WeakSet结构与Set类似,也是不重复的值的集合。但是,它与Set有两个区别。