Set
对象允许你存储任何类型的唯一值,无论是基本数据类型
还是对象引用
。
- Set中的严格类型检测,但是NaN被认为是相同的(尽管
NaN !== NaN
) - Set中的数据也是有序的。
简介
声明
使用new Set()
进行声明和创建一个对象。
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
"use strict"
let obj = {"name": "name1"};
let obj2 = {"sex": "sex"};
let set = new Set();
set.add(obj)
set.add(obj2)
console.dir(set) // Set(2)
</script>
</body>
</html>
Set中的属性
size:返回Set对象中值的个数
Set对象与数组的关系
Set对象与数组之间的相互转换
Set对象转化为数组
三点语法
或Array.from()
把Set对象转换为数组
<!DOCTYPE html>
<html>
<head>
<title>Map对象</title>
</head>
<body>
<div id="message">
</div>
<script>
"use strict"
let obj = {"obj": "对象"}
let set = new Set()
set.add(1);
set.add("2");
set.add(obj);
let [...setarray] = [...set];
console.dir(setarray) // Array(3)
let setarr = Array.from(set)
console.dir(setarr) // Array(3)
</script>
</body>
</html>
数组转化为Set对象
<!DOCTYPE html>
<html>
<head>
<title>Map对象</title>
</head>
<body>
<div id="message">
</div>
<script>
"use strict"
let array = [1,2,3,1,"2"]
let set = new Set(array)
console.dir(set) // Set(4)
</script>
</body>
</html>
Set的操作
增删查改、检测值
方法 | 作用 | 参数 | 返回值 |
---|---|---|---|
set.add(value) | 在Set对象的尾部添加元素 | 值 | Set对象 |
set.delete(value) | 删除Set对象的值 | 值 | Set对象有中此值,删除返回true;否则返回false |
set.clear() | 清空Set对象 | 无 | 无 |
set.has(value) | 根据值查看Set对象中是否有该值 | 值 | 存在返回true;否则返回false |
注意:
<!DOCTYPE html>
<html>
<head>
<title>Set对象</title>
</head>
<body>
<div id="message">
</div>
<script>
"use strict"
let obj = {"obj": "对象"}
let set = new Set()
set.add(1);
set.add("1");
set.add(obj);
console.log(set.has(obj)) // true
set.delete(obj)
console.log(set.has(obj)) // false
console.log(set.has("1")) // true
console.log(set) // {1, "1"}
</script>
</body>
</html>
遍历
方法 | 作用 | 参数 | 返回值 |
---|---|---|---|
set.values() | 返回Set对象的所有值 | 无 | values |
set.entries() | 返回Set对象的所有键值对 | 无 | key=value |
set.forEach() | set是迭代对象,可以直接遍历 | function(value, key, set) | 无 |
<!DOCTYPE html>
<html>
<head>
<title>Map对象</title>
</head>
<body>
<div id="message">
</div>
<script>
"use strict"
let obj = {"obj": "对象"}
let set = new Set()
set.add(1);
set.add("2");
set.add(obj);
for (let value of set.values()) {
console.log(value) // 1 2 {obj: "对象"}
}
for (let [value] of set.entries()) {
console.log(value) // 1 2 {obj: "对象"}
}
set.forEach(function(value){
console.log(value) // 1 2 {obj: "对象"}
})
</script>
</body>
</html>
集合的基本操作
交集
<!DOCTYPE html>
<html>
<head>
<title>Map对象</title>
</head>
<body>
<div id="message">
</div>
<script>
"use strict"
let hd = new Set(['hdcms', 'houdunren']);
let cms = new Set(['后盾人', 'hdcms']);
let newSet = new Set(
[...hd].filter(item => cms.has(item))
);
console.log(newSet); //{"hdcms"}
</script>
</body>
</html>
差集
<!DOCTYPE html>
<html>
<head>
<title>Map对象</title>
</head>
<body>
<div id="message">
</div>
<script>
"use strict"
let hd = new Set(['hdcms', 'houdunren']);
let cms = new Set(['后盾人', 'hdcms']);
let newSet = new Set(
[...hd].filter(item => !cms.has(item))
);
console.log(newSet); //{"houdunren"}
</script>
</body>
</html>
并集
<!DOCTYPE html>
<html>
<head>
<title>Map对象</title>
</head>
<body>
<div id="message">
</div>
<script>
"use strict"
let hd = new Set(['hdcms', 'houdunren']);
let cms = new Set(['后盾人', 'hdcms']);
let newSet = [...hd, ...cms];
console.log(newSet); // ["hdcms", "houdunren", "后盾人", "hdcms"]
</script>
</body>
</html>