Set数据结构
作用:自带去重功能(基本数据类型)。类数组,可以存储任意数据类型。
用法:new Set([数组/类数组])
示例代码:
<script>
// Set自带去重功能,只支持基本数据类型
let arr = [2,1,3,2,4,5,2,5,1,function(){},function(){}];
let arr1 = new Set(arr);
console.log(arr1);//{2, 1, 3, 4, 5,function(){}.function(){}}
</script>
运行结果:
Set常用方法:
-
add
作用:向set数据结构中添加元素数据,返回set数据结构本身。
用法:Set数据结构.add(需要添加的元素)
<script>
let arr = [2,1,3,2,4,5,2,5,1];
let arr1 = new Set(arr);
//add,向Set数据结构中添加元素,支持任意数据类型。
arr1.add(function(){});
console.log(arr1);//{2, 1, 3, 4, 5,function(){}}
</script>