美文网首页
es6的Set()构造函数

es6的Set()构造函数

作者: 秀萝卜 | 来源:发表于2020-09-07 08:52 被阅读0次

原文地址:https://www.cnblogs.com/douyaer/p/7942248.html

关于Set()函数

Set是一个构造器,类似于数组,但是元素没有重复的

1.接收数组或者其他iterable接口的数据 用于初始化数据
let a=new Set([1,32,424,22,12,3,2,2,1]);
console.log(a)//[ 1, 32, 424, 22, 12, 3, 2 ]
2. ☆实现数组去重
let b=[1,2,3,4,51,51,2,23,2];
let c=new Set(b);
b=[...c];
console.log(b);//[ 1, 2, 3, 4, 51, 23 ]
3. Set内默认NaN是相等的 遵循的是'==='
let d=new Set();
d.add(NaN);
d.add(NaN);
console.log(d);//[NaN]
4.Set()的方法

add()增加一个成员 返回set结构本身
delete()删除一个成员 返回bool
has()判断里面是否存在某个元素,返回bool
clear()清楚所有成员,没有返回值

let e=new Set();
let f=e.add(1);
console.log(e,f);//[1] [1]
let g=f.has(1);
console.log(g);//true
f.add(3);
console.log(f)//[1,3]
let h=f.delete(3);
console.log(h,f)//true [1]
f.clear();
console.log(f)//[]
5. Array.from()可以将set转换为数组
let i=new Set([1,2,3,4,5]);
console.log(Object.prototype.toString.call(Array.from(i)));//[object Array]
6.遍历方法 for...of

keys()
values()
entries()
forEach

let a=new Set([11,22,33,44]);
for(let i of a){
    console.log(i)
}// 11 22 33 44
for(let i of a.values()){
    console.log(i)
}//11 22 33 44
for(let i of a.keys()){
    console.log(i)
}//11 22 33 44
for(let i of a.entries()){
    console.log(i)
}//[11,11] [22,22] [33,33] [44,44]
a.forEach((key,value)=>{
    console.log(key,value)
})//11 11   22 22  33 33  44 44
7.weakSet()构造器只能存放对象

相关文章

  • Set和Map数据结构

    官方文档 http://caibaojian.com/es6/set-map.html Set是一个构造函数,类...

  • 构造函数相关笔记

    es5环境下写构造函数 继承 es6环境下的构造函数 继承 class的set和get clsss的静态方法

  • ES6之Set和Map数据结构

    ES6提供了新的数据结构Set,类似数组,元素值都是唯一的,不能重复。Set本身就是一个构造函数。 Set接受数组...

  • Vue 城市页面渲染

    一、数组去重 1、使用ES6中的 Set 构造函数 (1)new Set( ):类似于数组的对象,区别于它的成员都...

  • es6的Set()构造函数

    原文地址:https://www.cnblogs.com/douyaer/p/7942248.html 关于Set...

  • es6 Set

    ES6提供了数据结构Set。类似于数组,但是没有重复值。 Set本身是一个构造函数,用来生成Set数据结构 数组的...

  • Set

    概览 ES6 提供了新的数据结构 Set。它类似于数组,但是成员的值都是唯一的。 内容 1.构造函数 Set 本身...

  • ES6基本的语法(九) Set

    Set Set 是 ES6 提供的构造函数,能够造出一种新的存储数据的结构 只有属性值,成员值唯一 可以转成数组,...

  • ES6 Set、Map

    Set ES6提供了新的数据结构Set。它类似于数组。但是成的值都是唯一的,没有重复的值。Set本身是一个构造函数...

  • ES6中set和map数据结构的操作方法

    set的属性和方法: 属性: Set.prototype.constructor:构造函数,默认就是Set函数。 ...

网友评论

      本文标题:es6的Set()构造函数

      本文链接:https://www.haomeiwen.com/subject/gmwzsktx.html