-
Notifications
You must be signed in to change notification settings - Fork 0
/
美团面试.js
122 lines (115 loc) · 2.79 KB
/
美团面试.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
getType(123) // number
getType(undefined) // undefined
getType(null) // null
getType(() => {}) function
getType({}) // object
getType([]) // array
getType(new Date()) // date
*/
const getType = (param) => {
if (param === null) return 'undefined'
if (typeof param === 'undefined') return 'undefined'
if (param instanceof Date ) return 'date'
if (param instanceof Number ) return 'number'
if (param instanceof Function ) return 'function'
if (Array.isArray(param)) return 'array'
if (typeof param === 'object') return 'object'
}
console.log(getType([]));
// parseUrl('https://www.meituan.com/index.html?a=1&b=2')
const parseUrl = (url) => {
const obj = {}
if (url.includes('?')) {
const urlParams = url.split('?')[1].split('#')[0]
const paramsArr = urlParams.split('&')
paramsArr.forEach(p => {
const param = p.split('=')
obj[param[0]] = param[1]
})
}
return obj
}
console.log(parseUrl('https://www.meituan.com/index.html?a=1&b=2'));
const list = [
{
value: '1',
children: [
{
value: '1.1',
},
{
value: '1.2',
used: false,
},
{
value: '1.3',
children: [
{
value: '1.3.1',
},
{
value: '1.3.2',
used: false,
},
{
value: '1.3.3',
},
],
},
],
},
{
value: '2',
used: false,
children: [
{
value: '2.1',
},
{
value: '2.2',
used: false,
},
],
},
{
value: '3',
used: true,
children: [
{
value: '3.1',
},
{
value: '3.2',
used: false,
},
],
},
]
const deleteUsed = (list) => {
const newList = [...list]
newList.forEach((item, index) => {
if (item.used === false) {
newList.splice(index, 1)
}
if (item.children && Array.isArray(item.children)) {
item.children = deleteUsed(item.children)
}
})
return newList
}
console.log(JSON.stringify(deleteUsed(list)));
// console.log(JSON.stringify(list))
const t = (cb, params, time) => {
let timer = null
let count = 0
return (cb, params) => {
if (timer) clearTimeout(timer)
if (count === 0) cb && cb(params)
count++
timer = setTimeout(() => {
cb && cb(params)
count = 0
}, time)
}
}