回答編集履歴

1

chousei

2022/10/20 08:42

投稿

yambejp
yambejp

スコア114876

test CHANGED
@@ -4,3 +4,52 @@
4
4
  console.log(obj); // {a:5,b:6}
5
5
  ```
6
6
  おそらくjsonデータの作り方が間違えているのでしょう。
7
+ もしこんなふうなjsonデータとしたらregionはこうやって得てください
8
+ ```javascript
9
+ const json={
10
+ "hoge":[
11
+ {
12
+ "no":"001",
13
+ "date":[
14
+ {
15
+ "region":"001-1",
16
+ "detail":{
17
+ "name":"タナカ",
18
+ "type":"C",
19
+ "weight":"40kg",
20
+ "height":"177m",
21
+ }
22
+ },{
23
+ "region":"001-2",
24
+ "detail":{
25
+ "name":"ヨシダ",
26
+ "type":"C",
27
+ "weight":"53kg",
28
+ "height":"164m",
29
+ }
30
+ },{
31
+ "region":"001-3",
32
+ "detail":{
33
+ "name":"サトウ",
34
+ "type":"C",
35
+ "weight":"70kg",
36
+ "height":"180m",
37
+ }
38
+ }
39
+ ]
40
+ }
41
+ ]
42
+ }
43
+ const getAllValues=(x)=>{
44
+ var ret=[];
45
+ Object.entries(x).forEach(y=>{
46
+ if(y[1] instanceof Object){
47
+ ret=ret.concat(getAllValues(y[1]));
48
+ }else{
49
+ ret.push(y);
50
+ }
51
+ });
52
+ return ret;
53
+ };
54
+ console.log(getAllValues(json).filter(x=>x[0]=="region").map(x=>x[1]));
55
+ ```