学无先后,达者为师

网站首页 编程语言 正文

Es6中用Set去重

作者:老王学编程 更新时间: 2022-01-15 编程语言

set是一种新的数据结构 是一个类数组。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
</head>

<body>
    <script>
        // set是一种新的数据结构 是一个类数组
        let arr = [1, 2, 3, 4, 5, 5, 4, 3];
        let res = [...new Set(arr)];

        console.log(res);
        //数组去重

        // map也是一种新的数据结构 也是键值对的集合 键值可以是任何的数据类型 可以是数字字符串对象 symbol 等等 但是我们之前的对象的key值只能是字符串
    </script>
</body>

</html>

f12打开控制台,看效果

去重过后一共有五个不重复的,length代表的是个数。

原文链接:https://blog.csdn.net/weixin_52063276/article/details/122284807

栏目分类
最近更新