From 474e096e07601f010e8212916ab8b448361f2290 Mon Sep 17 00:00:00 2001 From: YangJ0605 <1442122744@qq.com> Date: Fri, 12 Jun 2020 18:10:24 +0800 Subject: [PATCH] map set --- "set\344\270\216map/map.js" | 7 +++++++ "set\344\270\216map/set.js" | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 "set\344\270\216map/map.js" create mode 100644 "set\344\270\216map/set.js" diff --git "a/set\344\270\216map/map.js" "b/set\344\270\216map/map.js" new file mode 100644 index 0000000..3513e78 --- /dev/null +++ "b/set\344\270\216map/map.js" @@ -0,0 +1,7 @@ +// map解决了js对象的键只能是字符串的问题 +const m = new Map() +const o = { name: 'object' } +m.set(o, 1) +m.get(o) + +console.log(m.has(o)) \ No newline at end of file diff --git "a/set\344\270\216map/set.js" "b/set\344\270\216map/set.js" new file mode 100644 index 0000000..8ee6a1a --- /dev/null +++ "b/set\344\270\216map/set.js" @@ -0,0 +1,24 @@ +// Set函数可以接受一个数组(或者具有 iterable 接口的其他数据结构)作为参数,用来初始化。 + +// let set = new Set() +// let a = NaN +// let b = NaN +// set.add(a).add(b).add(1) +// console.log(set) +// console.log(set.size) +// console.log(set.keys()) +// console.log(set.values()) + + +// 数组并集 +let arr = [1, 2, 3, 4, 5] +let arr2 = [3, 4, 5, 78, 9] +console.log([...new Set([...arr, ...arr2])]) + +// 数组交集 +console.log(arr.filter(item => new Set(arr2).has(item))) + +// WeakSet 只能存放对象 +const ws = new WeakSet() +ws.add({ a: 1 }) +console.log(ws) \ No newline at end of file