美文网首页
Set数据结构

Set数据结构

作者: Clayten | 来源:发表于2018-04-03 16:07 被阅读0次

1.Set(集合):类似与数组,但成员的值是唯一的,不重复的

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        let s = new Set();
        s.add(10);
        s.add("hello");
        s.add(10);
        console.log(s,s.size);
        //判断它的长度使用size属性
    </script>
</body>
</html>

2.数组转化为集合

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        let arr = [10,20,10,20,30,40];
        // let s = new Set();
/*      for (let i in arr){
            s.add(arr[i]);
        }
        console.log(s);*/
/*      arr.forEach(x => {
            s.add(x);
        })
        console.log(s);*/
        let s = new Set(arr);
        let arr1 = Array.from(s);//集合转化为数组
    </script>
</body>
</html>

3.set常用操作

3-1.add:添加成员

3-2:delete:删除成员

3-3:clear:清空

遍历set: forEach();

相关文章

  • new Set去重的用法

    使用Set数据结构去除重复对象:new Set(strings)进行转型。因为Set数据结构并非真正的数组,它类似...

  • 【List、Set、数据结构、Collections】

    【List、Set、数据结构、Collections】 主要内容 数据结构 List集合 Set集合 Collec...

  • ES6中的Set和Map

    ES6中新增了Set、WeakSet、Map、WeakMap数据结构 一、Set Set是类似数组的数据结构,和数...

  • Redis数据结构

    Redis数据结构:有五种数据结构:Strings,Hansh,List,Set,Sorted Set Strin...

  • # day03 【List、Set、数据结构、Collectio

    day03 【List、Set、数据结构、Collections】 主要内容 数据结构 List集合 Set集合 ...

  • 步遥——Set和Map数据结构

    Set (集合)和Map (映射) 1:Set数据结构:Set新的数据结构,类似数组,但成员的值都是唯一的,没有重...

  • ES6中Set和Map数据结构

    Set 数据结构 一、基本用法 1、数据结构Set,类似于数组,成员的值都是唯一的,没有重复的值。 2、Set本身...

  • Set Map WeakSet WeakMap

    Set 集合的数据结构 Set集合新增了add, delete ,Set.size,has,keys,clear,...

  • 10-redis

    Redis: 1、数据结构:string, hash, list, set, sorted set 2、memca...

  • es6 Set

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

网友评论

      本文标题:Set数据结构

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