码迷,mamicode.com
首页 > 其他好文 > 详细

Set 和 Map 数据结构

时间:2017-12-08 18:22:34      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:sha   pre   foreach   func   each   console   并集   union   fun   

set 它类似于数组,但是成员的值都是唯一的,没有重复的值。

方法:add(),has(),delete(),clear()

 

转为对像:

const items = new Set([1, 2, 3, 4, 5]);
const array = Array.from(items);

 

去除数组重复成员:

function dedupe(array) {
  return Array.from(new Set(array));
}

dedupe([1, 1, 2, 3]) // [1, 2, 3]

遍历:

const items = new Set([1, 2, 3, 4, 5]);
items.forEach((val)=>{
	console.log(val)
})

数组操作:

let a = new Set([1, 2, 3]);
let b = new Set([4, 3, 2]);

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

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

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

  

 

 

Map

Set 和 Map 数据结构

标签:sha   pre   foreach   func   each   console   并集   union   fun   

原文地址:http://www.cnblogs.com/fm060/p/8006376.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!