0
点赞
收藏
分享

微信扫一扫

Es6 去重方法

花海书香 2022-04-21 阅读 51
javascript

去重:包括数组,字符串去重

    // es6 set方法
    let arr = [1, 4, 5, 1, 4, 3, 1, 7]
    let arr1 = [2, 7, 9]
    let arr2 = [8, 0];
    let str = 'abcdefgacd';
    //去重
    console.log([...new Set(arr)]);//数组去重简写
    //[1, 2, 3, 4, 5, 7, 8, 9, 0]
    console.log(Array.from(new Set(arr)));//Array.from()将对象或其他转换为字符串
    //[1, 2, 3, 4, 5, 7, 8, 9, 0]
    console.log([...new Set(str)]);//字符串去重
    //["a", "b", "c", "d", "e", "f", "g"]
    console.log([...new Set(str)].join());//拼接字符串,将数组转换为字符串,join中不填写内容默认以逗号拼接
    //a,b,c,d,e,f,g
    console.log([...new Set(str)].join(''));
    //abcdefg
举报

相关推荐

0 条评论