Skip to content

Commit

Permalink
map set
Browse files Browse the repository at this point in the history
  • Loading branch information
abstain23 committed Jun 12, 2020
1 parent 000a70a commit 474e096
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions set与map/map.js
Original file line number Diff line number Diff line change
@@ -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))
24 changes: 24 additions & 0 deletions set与map/set.js
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 474e096

Please sign in to comment.