-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (46 loc) · 1.19 KB
/
index.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
class Recursive {
objectCheck(object, inheritanceKey = '') {
// eslint-disable-next-line no-restricted-syntax
for (const [key, value] of Object.entries(object)) {
if (typeof value === 'object' && !Array.isArray(value)) {
// NOTE: 檢查的值是物件(排除陣列),則再進入物件內層進行檢查
let newInput = '';
if (inheritanceKey === '') {
newInput = key;
} else {
newInput = `${inheritanceKey}.${key}`;
}
this.objectCheck(value, newInput);
} else {
// NOTE: 檢查的值非物件,則進行資料檢查
let searchKey = '';
if (inheritanceKey === '') {
searchKey = key;
} else {
searchKey = `${inheritanceKey}.${key}`;
}
console.log(`${searchKey} =`, value, `, type: ${typeof value === 'object' ? 'array' : typeof value}`);
}
}
}
}
const recursive = new Recursive();
const object = {
a: {
b: {
c: {
d: {
e: {
f: {
g: 111,
},
},
},
d1: true,
},
},
b1: [1, 2, 3, 4, 5],
},
a1: '12345',
};
recursive.objectCheck(object);