ES6新增语法——Set和Map
2020-5-18
非数组对象的定制实现,即 Set 与 Map
ES6 提供了新的数据结构 Set。它类似于数组,但是成员的值都是唯一的,没有重复的值*
{
const s = new Set();
[1,2,2,2,6,5].forEach(x=>s.add(x));
for(let i of s){
console.log(i);
}
//ES6 提供了新的数据结构 Set。它类似于数组,但是成员的值都是唯一的,没有重复的值
}
去重
{
let set = new Set([1,2,3,4,4]);
console.log([...set]);
let items = new Set([1,2,3,3,3,5,3])
console.log(...items);
//去掉数组中的重复字符
let str = "abbbcc"
console.log([...new Set(str)].join(""));
//去掉字符串中的重复字符
}