-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path编程-基础-算法.js
51 lines (47 loc) · 1.24 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
// 函数实现
const obj = {
a: {
ab: {
abb: 'a11',
ab: 'a12'
},
ad: {
a21: 'a21',
a2x: 'a22'
}
},
b: {
b_1: 'b1',
b_2: 'b2'
},
c: 'c'
}
function findPath() {
// 实现
}
findPath(obj, 'a22') // ['a', 'ab', 'a2x']
// 这个应该是考算法实现
//======================参考答案=======================
// zl - 23.02.07
function ans(obj, target) {
// 深度遍历
const o = {}
const find = (obj, target, path = []) => {
for (const key in obj) {
const val = obj[key]
if (val === target) {
return [...path, key]
}
if (typeof val !== 'object') continue // 不是object类型
if (val === null) continue // null特殊情况
if (!Object.getPrototypeOf(val) && Object.getPrototypeOf(o) !== Object.getPrototypeOf(val)) continue // 不是plain object
// Object.create(null) -> {}
// Object.getPrototypeof(Object.create(null)) -> null
const res = find(val, target, [...path, key])
if (!res) continue
return res
}
}
const res = find(obj, target) || []
return res
}