set和map
ts
let m = setInterval(() => console.log('Hi'), 1000)
console.log(m)
let m = setInterval(() => console.log('Hi'), 1000)
console.log(m)
ts
for (const [i, v] of [`a`, `b`, `c`].entries()) {
console.log(i, v)
}
// 例子 6-2
const arr = [
[234, 5],
[6, 78],
[4, 5],
]
const map = new Map(arr)
// 遍历 key 值
for (const key of map.keys()) {
console.log(key)
}
// 遍历 value 值
for (const value of map.values()) {
console.log(value)
}
// 遍历 key 和 value 值(一)
for (const item of map.entries()) {
console.log(item[0], item[1])
}
const data = new Map(arr)
// 遍历 key 和 value 值(二)
for (const [key, value] of data) {
console.log(key)
}
export {}
for (const [i, v] of [`a`, `b`, `c`].entries()) {
console.log(i, v)
}
// 例子 6-2
const arr = [
[234, 5],
[6, 78],
[4, 5],
]
const map = new Map(arr)
// 遍历 key 值
for (const key of map.keys()) {
console.log(key)
}
// 遍历 value 值
for (const value of map.values()) {
console.log(value)
}
// 遍历 key 和 value 值(一)
for (const item of map.entries()) {
console.log(item[0], item[1])
}
const data = new Map(arr)
// 遍历 key 和 value 值(二)
for (const [key, value] of data) {
console.log(key)
}
export {}
ts
// 例子 5-2
// 根据颜色找出对应的水果
// Bad
function test1(color) {
switch (color) {
case `red`:
return [`apple`, `strawberry`]
case `yellow`:
return [`banana`, `pineapple`]
case `purple`:
return [`grape`, `plum`]
default:
return []
}
}
test1(`yellow`) // ['banana', 'pineapple']
// Good
const fruitColor1 = {
red: [`apple`, `strawberry`],
yellow: [`banana`, `pineapple`],
purple: [`grape`, `plum`],
}
function test2(color) {
return fruitColor1[color] || []
}
// Better
const fruitColor2 = new Map()
.set(`red`, [`apple`, `strawberry`])
.set(`yellow`, [`banana`, `pineapple`])
.set(`purple`, [`grape`, `plum`])
function test3(color) {
return fruitColor2.get(color) || []
}
export {}
// 例子 5-2
// 根据颜色找出对应的水果
// Bad
function test1(color) {
switch (color) {
case `red`:
return [`apple`, `strawberry`]
case `yellow`:
return [`banana`, `pineapple`]
case `purple`:
return [`grape`, `plum`]
default:
return []
}
}
test1(`yellow`) // ['banana', 'pineapple']
// Good
const fruitColor1 = {
red: [`apple`, `strawberry`],
yellow: [`banana`, `pineapple`],
purple: [`grape`, `plum`],
}
function test2(color) {
return fruitColor1[color] || []
}
// Better
const fruitColor2 = new Map()
.set(`red`, [`apple`, `strawberry`])
.set(`yellow`, [`banana`, `pineapple`])
.set(`purple`, [`grape`, `plum`])
function test3(color) {
return fruitColor2.get(color) || []
}
export {}
ts
const s = new Set()
;[1, 2, 34, 5].forEach((x) => {
s.add(x)
})
for (const i of s) {
const m = 343
console.log(m)
console.log(i)
}
console.log([...new Set(`aaadddbdde`)].join(``)) //去除重复字符
const prop = new Set()
prop.add(`hello`)
prop.add(`lisck`)
if (prop.has(`hello`)) {
console.log(`Hello`)
}
const y = new Set([`red`, `green`, `blue`])
for (const string of y) {
console.log(string)
}
const foos = new WeekSet()
class Foo {
constructor() {
foos.add(this)
}
method() {
if (!foos.has(this)) {
console.log(`helo`)
}
}
}
const s = new Set()
;[1, 2, 34, 5].forEach((x) => {
s.add(x)
})
for (const i of s) {
const m = 343
console.log(m)
console.log(i)
}
console.log([...new Set(`aaadddbdde`)].join(``)) //去除重复字符
const prop = new Set()
prop.add(`hello`)
prop.add(`lisck`)
if (prop.has(`hello`)) {
console.log(`Hello`)
}
const y = new Set([`red`, `green`, `blue`])
for (const string of y) {
console.log(string)
}
const foos = new WeekSet()
class Foo {
constructor() {
foos.add(this)
}
method() {
if (!foos.has(this)) {
console.log(`helo`)
}
}
}