Set WeakSet Map WeakMap
2019-10-06 本文已影响0人
Aniugel
数据结构
数组
json, 二叉树....
set数据结构:
类似数组,但是里面不能有重复值
let arr = ['a','b','a'];
let arr = new Array();
set用法:
let setArr = new Set(['a','b']);
setArr.add('a'); 往setArr里面添加一项
setArr.delete('b'); 删除一项
setArr.has('a') 判断setArr里面有没有此值
setArr.size 个数
setArr.clear(); 清空
for...of...
循环:
a). for(let item of setArr){ //默认是values()
console.log(item);
}
b). for(let item of setArr.keys()){console.log(item);}
c). for(let item of setArr.values()){}
d). for(let [k,v] of setArr.entries()){}
e). setArr.forEach((value,index) =>{
console.log(value, index);
});
let setArr = new Set().add('a').add('b').add('c');
数组去重:
let arr = [1,2,3,4,5,6,7,6,5,4,3,2,1,2,3,4,4];
let newArr = [...new Set(arr)];
console.log(newArr);
set数据结构变成数组:
[...set]
想让set使用数组的,map循环和filter:
--------------------------------------------------------------
let arr = [{},{}];
--------------------------------------------------------------
new Set([]); 存储数组, 这种写法对
new WeakSet({}) 存储json,这种写法不靠谱
WeakSet没有size,也没有clear()
有, add(), has(), delete()
确认,初始往里面添加东西,是不行的。最好用add添加
--------------------------------------------------------------
总结: new Set()
--------------------------------------------------------------
let json ={
p1:1,
b:2
};
--------------------------------------------------------------
map:
类似 json, 但是json的键(key)只能是字符串
map的key可以是任意类型
使用:
let map = new Map();
map.set(key,value); 设置一个值
map.get(key) 获取一个值
map.delete(key) 删除一项
map.has(key) 判断有没有
map.clear() 清空
循环:
for(let [key,value] of map){}
for(let key of map.keys()){}
for(let value of map.values()){}
for(let [k,v] of map.entries()){}
map.forEach((value, key) =>{
console.log(value, key);
})
WeakMap(): key只能是对象
------------------------------------------------------------
总结:
Set 里面是数组,不重复,没有key,没有get方法
Map 对json功能增强,key可以是任意类型值
------------------------------------------------------------
Set SetWeak
// console.log(typeof setArr)//object
// console.log(setArr)//object
// console.log([...setArr])//["a", "b", "c"]
// console.log(Array.from(setArr))//["a", "b", "c"]
// let arr = [1, 2, 3, 1]
// console.log(new Set(arr))// 去重
let setArr = new Set()
setArr.add('a')
setArr.add('b')
setArr.add('c')
console.log(setArr)
// delete 删除元素
setArr.delete('b')//删除
setArr.delete('d')//无效
console.log(setArr)
// has
console.log(setArr.has('a'))//true
console.log(setArr.has('d'))//false
// size
console.log(setArr.size)//2
// clear全部删除
setArr.clear()
console.log(setArr.size)//0
console.log('-----------------')//0
let arr = new Set(['a', 'b', 'c', 'd']);
for (let item of arr) {
console.log(item);//a b c d
}
for (let item of arr.keys()) {
console.log(item);//a b c d
}
for (let item of arr.values()) {
console.log(item);//a b c d
}
// value和key一样
for (let [k, v] of arr.entries()) {
console.log(k, v);//a a b b c c d d
}
arr.forEach((value, index) => {
console.log(value, index)
})
// let newArr = new Set()
// newArr.add('a').add('b').add('c')
// console.log(newArr)//Set(3) {"a", "b", "c"}
// 增加简化
let newArr = new Set().add('a').add('b').add('c')
console.log(newArr)//Set(3) {"a", "b", "c"}
// 赋值
let set = new Set([1, 2, 3])
// let set2 = new Set()
// for (let val of set) {
// set2.add(val * 2)
// }
let set2 = new Set([...set].map(val => val * 2))
console.log(set2)//Set(3) {2, 4, 6}
// let set1 = new Set()
// let json = {
// a: 1,
// b: 2
// }
// let json2 = {
// a: 3,
// b: 4
// }
// set1.add(json)
// set1.add(json2)
// console.log(set1)
// for (let val of set1) {
// console.log(val.a)// 1 3
// }
// let set1 = new WeakSet({ a: 1, b: 2 })//报错
let set1 = new WeakSet()//存储一个json 不能直接放 没有size 没有clear
let json = {
a: 1,
b: 2
}
set1.add(json)
console.log(set1)
Map WeakMap
let map = new Map()
let json = {
a: 1,
b: 2
}
map.set('a', 'aaa')
map.set(json, 'bbb')
map.set('aaa', json)
console.log(map.get('a'))//aaa
console.log(map.get(json))
console.log(map.get('aaa'))
console.log('---------------')
for (const [key, value] of map) {
console.log(key, value)
}
map.delete('aaa')
console.log(map)//Map(2) {"a" => "aaa", {…} => "bbb"}
console.log(map.has('a'))//true
map.clear()
console.log(map)//Map(0) {}
// WeakMap key只能是对象 josn只能是支付串 Set什么都可以
let wMap = new WeakMap()
let json1 = {
a: 1,
b: 2
}
wMap.set(json1, 'aaa')
console.log(wMap)