teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

2021/06/25 20:32

投稿

k49977
k49977

スコア27

title CHANGED
File without changes
body CHANGED
@@ -52,4 +52,56 @@
52
52
 
53
53
  ###試したこと
54
54
  以下のように実装しています。
55
- map,flatmap,filterを使ってやろうとしています。またコードも載せたいと思います。
55
+ map,flatmap,filterを使ってやろうとしています。
56
+
57
+ 以下検討中
58
+
59
+ ```js
60
+ let a =
61
+ // APIからのレスポンスJSON
62
+ [
63
+ {
64
+ id: 1,
65
+ name: "甲グループ",
66
+ custom: [
67
+ {
68
+ id: 1,
69
+ name: "甲-1",
70
+ type: "B"
71
+ }
72
+ ]
73
+ },
74
+ {
75
+ id: 2,
76
+ name: "乙グループ",
77
+ custom: [
78
+ {
79
+ id: 2,
80
+ name: "乙-1",
81
+ type: "A"
82
+ },
83
+ {
84
+ id: 3,
85
+ name: "乙-2",
86
+ type: "B"
87
+ }
88
+ ]
89
+ }
90
+ ];
91
+
92
+ const target = "B";
93
+ const result = a.map(it=>{
94
+ it.custom.filter(x=>{
95
+ console.log("1");
96
+ console.log(JSON.stringify(it));
97
+ return x.type==target;
98
+ });
99
+ }).map(y=>{
100
+ console.log("2");
101
+ console.log(JSON.stringify(y));
102
+ return y.id;
103
+ })
104
+ .flatMap(z=>z);
105
+
106
+ console.log(JSON.stringify(result));
107
+ ```