回答編集履歴
2
2
test
CHANGED
@@ -23,3 +23,31 @@
|
|
23
23
|
console.log(get_data(player,["detail","age"]))
|
24
24
|
|
25
25
|
```
|
26
|
+
|
27
|
+
--- 追記 ---
|
28
|
+
|
29
|
+
もしご質問のような書式を使いたいなら
|
30
|
+
|
31
|
+
```javascript
|
32
|
+
|
33
|
+
function get_data(player, key) {
|
34
|
+
|
35
|
+
let [key1,key2] = key.split('.')
|
36
|
+
|
37
|
+
if (typeof key2 == "undefined") return player[key]
|
38
|
+
|
39
|
+
return player[key1][key2]
|
40
|
+
|
41
|
+
}
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
let player = { "name": "Ronaldo", "team": "Juventus" , "detail": { "age":23, "height":180 }}
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
console.log(get_data(player,"name"))
|
50
|
+
|
51
|
+
console.log(get_data(player,"detail.age"))
|
52
|
+
|
53
|
+
```
|
1
1
test
CHANGED
@@ -6,7 +6,9 @@
|
|
6
6
|
|
7
7
|
if (typeof key == "string") return player[key]
|
8
8
|
|
9
|
+
let [key1,key2] = key
|
10
|
+
|
9
|
-
return player[key
|
11
|
+
return player[key1][key2]
|
10
12
|
|
11
13
|
}
|
12
14
|
|