// ES6 Set
let instruments = new Set();
// or 一開始就把值存進去
let instruments = new Set(['1','2']);
instruments.add('piano');
instruments.has('guitar'); // false
instruments.delete('guitar'); // 裡面根本沒有 guitar 所以回傳 false
instruments.size; // 1
[...instruments] //['piano']
// or use Array.from,
Array.from(instruments);
let myMap= new Map();
// or 一開始就存值,記得是 [[]] double array
let myMap = new Map([[keyString ,'value']]);
var keyString = 'I am string',
keyObj = {},
keyFunc = function() {},
keyNumber = 1
// 增加
myMap.set(keyString , 'string value');
myMap.set(keyObj, {obj: 1});
myMap.set(keyFunc , function(){console.log('I am function')});
myMap.set(keyNumber , 100);
// 有幾個
myMap.size; // 4
// 取值
myMap.get(keyObj); // {obj: 1}
// 看是否存在
myMap.has(keyString ); // true
// 刪掉
myMap.delete(keyNumber);
myMap.size; // 3
// 轉陣列
[...myMap.values()] // ["string value", {obj: 1}, ƒ]
if( lookup.has(str) ){
lookup.set(str, lookup.get(str) + 1 )
} else {
lookup.set(str, 1 )
}
lookup.set(str, (lookup.get(str) ?? 0) + 1 )
if( !lookup.has(sortStr) ){
lookup.set(sortStr, [str] )
} else {
lookup.set(sortStr, [...lookup.get(sortStr), str] )
}
// 可以變以下
lookup.set(sortStr, [...(lookup.get(sortStr) ?? []), str] )