-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjectPractice.js
86 lines (74 loc) · 1.78 KB
/
objectPractice.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
// 객체 실습1
const widget = {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}
const numberKeys = [];
for (let key in widget) {
const value = widget[key];
if (typeof value === 'object') {
for (let key2 in value) {
const value2 = value[key2];
if (typeof value2 === 'number') {
numberKeys.push(key2);
}
}
}
}
console.log(numberKeys);
const valueArr = [];
for (let value in widget) {
if (typeof widget[value] === 'object') {
for (let value2 in widget[value]) {
if (typeof widget[value][value2] === 'number') {
valueArr.push(value2);
}
}
}
}
console.log(valueArr);
// better solution (from Head & Kaka)
/*
var result = [];
for (v in widget) {
if (typeof widget[v] === 'object') {
for (x in widget[v]) {
if (typeof widget[v][x] === 'number') {
result.push(x)
}
}
}
}
*/
// better solution (from Torco & besense)_ recursive
/*
const new_arr = [];
function makeArr(obj){
for(const key in obj){
(typeof obj[key] === 'object' || typeof obj[key] === 'array') ? makeArr(obj[key]) : typeof obj[key] === 'number' ? new_arr.push(key) : '';
}
return new_arr;
}
*/