zoukankan      html  css  js  c++  java
  • Es6中关于Set新特性的应用练习(Set集合对于数组的操作)

    Es6中关于Set新特性的应用练习

    1. 数组的去重

      方案一:

      let arr1 = [1, 1, 3, 4, 6, 7, 4, 9]
      let s= new Set(arr1) // 这里得到的值并不是最终去重后的数组
      let newArray=Array.from(s)
      

    注意:new Set() 的返回值类型是一个Object类型,所以第二步得到的并不是最终去重后的代码

    ​ 方案二:利用Es6的展开运算符

    		let arr1 = [1, 1, 3, 4, 6, 7, 4, 9]
    		//  数组去重
    		let s = [...new Set(arr1)] // 将对象直接展开放入数组中
    
    1. 取出两个数组中元素的交集

      let arr1 = [1, 1, 3, 4, 6, 7, 4, 9]
      let arr2 = [2, 3, 4, 5, 65, 7, 4, 1]
      //取数组中的元素的交集
      let contact = [...new Set(arr1)].filter((item) => new Set(arr2).has(item))
      console.log(contact)
      
    2. 取出两个数组中元素的并集

      let arr1 = [1, 1, 3, 4, 6, 7, 4, 9]
      let arr2 = [2, 3, 4, 5, 65, 7, 4, 1]
      //取出数组中元素的并集
      let union = [...new Set([...new Set(arr1), ...new Set(arr2)])]
      console.log(union)
      
    3. 取出两个数组中元素的差集

    
     let arr1 = [1, 1, 3, 4, 6, 7, 4, 9]
     let arr2 = [2, 3, 4, 5, 65, 7, 4, 1]
     //取出数组中元素的差集
    let diff = [...new Set(arr1)].filter((item) => !new Set(arr2).has(item))
    console.log(diff)
    

  • 相关阅读:
    windows安全实验
    ping 命令的禁止 以及密码的攻破
    网络基础
    html 中间件
    js php BurpSuite v2.1
    网页标签,PHPstudy
    说说text_line_orientation算子的巧妙应用
    说说C#进行数字图像处理的方法
    微信张小龙产品30条
    说说几个常用的阈值分割算子
  • 原文地址:https://www.cnblogs.com/comyan/p/13454915.html
Copyright © 2011-2022 走看看