-
Notifications
You must be signed in to change notification settings - Fork 0
/
腾讯面试.js
95 lines (83 loc) · 2.03 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
// 输入[1,2,2,[4,4,5,5],3,3] 输出 [1,2,[4,5],3];
function flattenArray(arr) {
const result = [];
arr.forEach((a) => {
if (Array.isArray(a)) {
const flattened = flattenArray(a);
result.push(flattened)
} else {
if (!result.includes(a)) result.push(a)
}
})
return result
}
console.log(flattenArray([1,2,2,[4,4,5,5],3,3]))
function flattenArray1(arr) {
const result = {};
arr.forEach((a) => {
if (!result[a]) {
result[a] = 1
} else {
result[a] += 1
}
})
const array = []
Object.keys(result).forEach((r) => {
if (result[r] === 2) {
array.push(Number(r))
}
})
console.log(array)
}
flattenArray1([ 1, 2, 2, 4, 4,5,5,5] )
function groupByFnc(arr) {
const obj = {}
arr.forEach((a) => {
if (obj[a.type]) {
obj[a.type].push(a)
} else {
obj[a.type] = [{...a}]
}
})
console.log(obj)
}
groupByFnc([{ name: 'car', type: 'toy' }, { name: 'rice', type: 'food' }, { name: 'dog', type: 'toy' }])
function cloneDeep(param) {
if (!param) return param
if (Array.isArray(param)) {
const newParamArray = []
param.forEach((p) => {
if (Array.isArray(p)) {
newParamArray.push(cloneDeep(p))
} else {
newParamArray.push(p)
}
})
return newParamArray
}
let newParam = {}
Object.keys(param).forEach((paramKey) => {
if (Array.isArray(param[paramKey])) {
newParam[paramKey] = cloneDeep(param[paramKey])
} else {
newParam[paramKey] = param[paramKey]
}
})
return newParam
}
let timer = null
function timerFnc(cb, params) {
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
//
cb && cb(params)
timer = null
}, 300)
}
function num(n) {
const numberStr = n.toLocaleString()
console.log(numberStr)
}
num(10000000)