• 技术文章 >Web开发 >JavaScript

    js数组对象去重es6方法

    宋雪维宋雪维2021-01-05 15:31:47原创6064

    es6全称ECMAScript 6,是JavaScript语言的国际标准,es6彻底改变程序员们编写js代码的方式,可以以简单的方式实现js数组去重。js本文介绍数组对象去重es6方法:使用es6的语法set去重和使用forEach去重。

    方式一:使用es6的语法set去重

    Map是es6提供的新的数据结构,set给Map对象设置key/value 键/值对。

    */
    function uuiq(arr) {
    let setArr= new Set(arr);
    let newArr = [...setArr];
    console.log(newArr);
    }

    方式二:使用forEach去重

    forEach可以对每个元素执行指定操作。

    let newEs6Json =[];
    obj2.forEach(item => {
        newEs6Json= obj1.filter(a  =>  a.spu != item.spu) 
     })
     console.log(newEs6Json );
     // [{"id": 0, "name": "王嘉尔"},{"id": 1,"name": "段宜恩"}]

    以上就是js数组对象去重es6方法实现的两种方式,快尝试看看吧~

    专题推荐:js数组
    品易云
    上一篇:json数组字符串转json对象 下一篇:jsonp怎么读

    相关文章推荐

    • 如何使用python中schedule模块?• 如何使用python中的optionparser模块?• python pexpect模块是什么?• python里glob模块怎么用?• python behold库是什么?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网