美文网首页
ES6基础之——new Set

ES6基础之——new Set

作者: 我是syq吖 | 来源:发表于2023-08-03 14:11 被阅读0次

类似于数组,et数据结构并非真正的数组,它类似于数组,并且成员值都是唯一的,没有重复
但是因为它是一个类似数组结构,所以需要转型为真正的数组去使用。所以需要用Array.from 或者使用es6解构

// new Set()里面需要接的是strings类型
 const list =[" { name: '张三', age: 18, address: '北京' }"," { name: '张三', age: 18, address: '北京' }"]
 console.log('去重',new Set(list))//{" { name: '张三', age: 18, address: '北京' }"}

如果里面不是一个string类型,而是对象不会去重:

// new Set()里面需要接的是strings类型
const list =[{ name: '张三', age: 18, address: '北京' },{ name: '张三', age: 18, address: '北京' }]
console.log('去重',new Set(list))

解决方法:因为数组里面是对象,所以我们这边先用map用JSON.stringify进行转化成string类型

const list =[
    { name: "张三", age: 18, address: "北京" },
    { name: "李四", age: 20, address: "天津" },
    { name: "张三", age: 18, address: "北京" },
]
const strings = list.map((item) => JSON.stringify(item))
console.log('strings', strings) //['{"name":"张三","age":18,"address":"北京"}', '{"name":"李四","age":20,"address":"天津"}', '{"name":"张三","age":18,"address":"北京"}']

然后,就可以用new Set对这个数组进行去重,然后再用Array.from转化为真正的数组,

打印出来这就已经去重了

const removeDupList = Array.from(new Set(strings))
console.log('removeDupList',removeDupList)//['{"name":"张三","age":18,"address":"北京"}', '{"name":"李四","age":20,"address":"天津"}']

然后,再把数组里面的字符串类型转化为对象类型:

const removeDupList = Array.from(new Set(strings))
const result = removeDupList.map((item) => JSON.parse(item))
console.log('result',typeof result)  // // [{name: '张三', age: 18, address: '北京'}, {name: '李四', age: 20, address: '天津'}]

const list =[
    { name: "张三", age: 18, address: "北京" },
    { name: "李四", age: 20, address: "天津" },
    { name: "张三", age: 18, address: "北京" },
]
 
const strings = list.map((item) => JSON.stringify(item)) // 1、转化成string类型
const removeDupList = Array.from(new Set(strings))  // 2、转化为真正的数组
const result = removeDupList.map((item) => JSON.parse(item)) // 3、字符串类型转化为对象类型
console.log('数组去重',result)  // [{name: '张三', age: 18, address: '北京'}, {name: '李四', age: 20, address: '天津'}]

Set 对象方法

add 添加某个值,返回Set对象本身。
clear 删除所有的键/值对,没有返回值。
delete 删除某个键,返回true。如果删除失败,返回false。
forEach 对每个元素执行指定操作。
has 返回一个布尔值,表示某个键是否在当前 Set 对象之中。

增删改查方法

1.添加元素add

添加某个值,返回 Set 结构本身,当添加实例中已经存在的元素,set不会进行处理添加

let list=new Set();
list.add(1)
list.add(2).add(3).add(3)  
 //  Set(3) {1, 2, 3}    3只被添加了一次
2.删除元素 delete

删除某个值,返回一个布尔值,表示删除是否成功

let list=new Set([1,20,30,40])
list.delete(30)      //删除值为30的元素,这里的30并非下标
console.log(list)  // Set(3) {1, 20, 40}
3.判断某元素是否存在has

返回一个布尔值,判断该值是否为Set的成员

let list=new Set([1,2,3,4])
console.log(list.has(2)) //true
4 清除所有元素clear

清除所有成员,没有返回值

let list=new Set([1,2,3,4])
list.clear()
console.log(list)   // Set(0) {size: 0}

3、遍历方法

3.1 遍历 keys()

返回键名的遍历器,相等于返回键值遍历器values()

let list = new Set(['a','b','c'])
for(let key of list.keys()){
   console.log(key)  //  'a','b','c'
}
3.2 遍历 values()

返回键值的遍历器

let list = new Set(['a','b','c'])
for(let value of list.values()){
  console.log(value)  //  'a','b','c'
}
3.3 遍历 entries()

返回键值对的遍历器

let list = new Set(['4','5','hello'])
for (let item of list.entries()) {
  console.log(item); 
// ['4','4']   ['5','5']   ['hello','hello'] 
}
3.3 遍历 forEach()

使用回调函数遍历每个成员

let list = new Set(['4','5','hello'])
list.forEach((value, key) => console.log(key + ' : ' + value)) // 4:4    5:5   hello:hello

4、使用情形

4.1 用于数组去重
var arr = [1,2,3,3,1,4];
[...new Set(arr)]; // [1, 2, 3, 4]
Array.from(new Set(arr)); // [1, 2, 3, 4]

[...new Set('ababbc')].join(''); // "abc" 字符串去重
new Set('ice doughnut'); //Set(11) {"i", "c", "e", " ", "d", …}
let arr = [3, 5, 2, 2, 5, 5];
let setArr = new Set(arr)  
console.log(setArr);  // 返回set数据结构  Set(3) {3, 5, 2}
//方法一   es6的...解构
let unique1 =  [...setArr ];    
console.log(unique1);    //去重转数组后  [3,5,2]
//方法二  Array.from()解析类数组为数组
let unique2 = Array.from(setArr ) 
console.log(unique2);   //去重转数组后  [3,5,2]
4.2 用于字符串去重
let str = "352255";
let unique = [...new Set(str)].join("");  
console.log(unique);  // 352 
4.3 实现并集、交集和差集
let a = new Set([1, 2, 3]);
let b = new Set([4, 3, 2]);

// 并集
let union = new Set([...a, ...b]);
console.log(union);  // Set(4) {1, 2, 3, 4}

// 交集
let intersect = new Set([...a].filter(x => b.has(x)));
console.log(intersect); // Set(2) {2, 3}

// (a 相对于 b 的)差集
let difference = new Set([...a].filter(x => !b.has(x)));
console.log(difference); // Set(1) {1}

相关文章

  • ES6基础之——new Set

    Set 对象存储的值总是唯一的 Set 对象方法方法 描述add 添加某个值,返回Set对象本身。clear ...

  • JS数组去重

    1、ES6语法filter()去重 2、ES6语法new Set()去重

  • JS常用的数组去重(和根据对象里的id去重)

    indexOf去重 ES6 Set去重 for嵌套for,然后splice去重 [...new Set(arr)]...

  • 前端面试题

    数组去重 1.利用ES6 Set去重(ES6中最常用) ...new set() 2.利用for嵌套for...

  • JS 常见的几种去重方法及原理

    # 1. ES6 语法 new Set() 方法去重 ES6 提供了新的数据结构 Set。它类似于数组,但是成员的...

  • JS 中常见的几种去重方法

    # 1. ES6 语法 new Set() 方法去重 ES6 提供了新的数据结构 Set。它类似于数组,但是成员的...

  • es6

    问:如何用es6给数组去重? 答: const set = new Set([1,2,3,4,4]) consol...

  • js数组去重常见的七种方法

    1、借助ES6提供的Set结构 new Set() 简单好用 强烈推荐 直接给一个新的数组里面,利用es6的延展运...

  • es6 new Set

    Set 对象存储的值总是唯一的 Set 对象方法 方法 描述add 添加某个值,返回Set对象本身。...

  • 数组-去重

    1、ES6: new Set() Set对象是值的集合,里面的元素唯一,不能去除空对象{}; 2、indexOf ...

网友评论

      本文标题:ES6基础之——new Set

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