質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

Q&A

解決済

1回答

517閲覧

jsonデータからidだけの配列を作りたい

k49977

総合スコア27

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

0グッド

0クリップ

投稿2021/06/25 13:12

編集2021/06/25 20:32

前提・実現したいこと

現在Reactの勉強をしています。

多次元連想配列?JSON形式?をtypeがAの場合のidのみ残した配列に編集したいのですがどうすればいいでしょうか?

具体的には以下です。
以下の配列をtypeがAのidの配列に編集したいです。

// APIからのレスポンスJSON.type=Bのcustom.idだけにしたい [ { "id": 1, "name": "甲グループ", "custom": [ { "id": 1, "name": "甲-1", "type": "B" } ] }, { "id": 2, "name": "乙グループ", "custom": [ { "id": 2, "name": "乙-1", "type": "A" }, { "id": 3, "name": "乙-2", "type": "B" } ] } ]

↓ こうしたい

// 期待値。typeがBだけのcustom.id配列 [1,3]

アドバイスいただけないでしょうか?

###試したこと
以下のように実装しています。
map,flatmap,filterを使ってやろうとしています。

以下検討中

js

1let a = 2 // APIからのレスポンスJSON 3 [ 4 { 5 id: 1, 6 name: "甲グループ", 7 custom: [ 8 { 9 id: 1, 10 name: "甲-1", 11 type: "B" 12 } 13 ] 14 }, 15 { 16 id: 2, 17 name: "乙グループ", 18 custom: [ 19 { 20 id: 2, 21 name: "乙-1", 22 type: "A" 23 }, 24 { 25 id: 3, 26 name: "乙-2", 27 type: "B" 28 } 29 ] 30 } 31 ]; 32 33const target = "B"; 34const result = a.map(it=>{ 35it.custom.filter(x=>{ 36console.log("1"); 37console.log(JSON.stringify(it)); 38return x.type==target; 39}); 40}).map(y=>{ 41console.log("2"); 42console.log(JSON.stringify(y)); 43return y.id; 44}) 45.flatMap(z=>z); 46 47console.log(JSON.stringify(result));

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

最終形態が以下です。

js

1let a = 2 // APIからのレスポンスJSON 3 [ 4 { 5 id: 1, 6 name: "甲グループ", 7 custom: [ 8 { 9 id: 1, 10 name: "甲-1", 11 type: "B" 12 } 13 ] 14 }, 15 { 16 id: 2, 17 name: "乙グループ", 18 custom: [ 19 { 20 id: 2, 21 name: "乙-1", 22 type: "A" 23 }, 24 { 25 id: 3, 26 name: "乙-2", 27 type: "B" 28 }, 29 { 30 id: 4, 31 name: "乙-3", 32 type: "B" 33 } 34 ] 35 }, 36 { 37 id: 3, 38 name: "龍グループ", 39 custom: [ 40 { 41 id: 5, 42 name: "龍-1", 43 type: "A" 44 } 45 ] 46 } 47 ]; 48 49const target = "B"; 50const result = a 51 .flatMap((x) => x.custom.filter((c) => c.type === target)) 52 .map((y) => y.id); 53 54console.log(JSON.stringify(result)); 55// [1,3,4] 56

最終形態までの経緯(上から下へ新しくなっていきます)

一応以下でいけました。(もっといい方法ないかな。。y[0]あたりが怪しい)
https://codesandbox.io/s/icy-cdn-jfbb3?file=/src/index.js

js

1let a = 2 // APIからのレスポンスJSON 3 [ 4 { 5 id: 1, 6 name: "甲グループ", 7 custom: [ 8 { 9 id: 1, 10 name: "甲-1", 11 type: "B" 12 } 13 ] 14 }, 15 { 16 id: 2, 17 name: "乙グループ", 18 custom: [ 19 { 20 id: 2, 21 name: "乙-1", 22 type: "A" 23 }, 24 { 25 id: 3, 26 name: "乙-2", 27 type: "B" 28 } 29 ] 30 } 31 ]; 32 33const target = "B"; 34console.log(JSON.stringify(a)); 35 36const result = a 37 .map((x) => x.custom.filter((c) => c.type === target)) 38 .flatMap((y) => y[0].id); 39 40console.log(JSON.stringify(result)); 41

追記

y[0]がnullだとidがとれないので対応.リファクタリングできそうな気がする

js

1let a = 2 // APIからのレスポンスJSON 3 [ 4 { 5 id: 1, 6 name: "甲グループ", 7 custom: [ 8 { 9 id: 1, 10 name: "甲-1", 11 type: "B" 12 } 13 ] 14 }, 15 { 16 id: 2, 17 name: "乙グループ", 18 custom: [ 19 { 20 id: 2, 21 name: "乙-1", 22 type: "A" 23 }, 24 { 25 id: 3, 26 name: "乙-2", 27 type: "B" 28 } 29 ] 30 }, 31 { 32 id: 3, 33 name: "龍グループ", 34 custom: [ 35 { 36 id: 4, 37 name: "龍-1", 38 type: "A" 39 } 40 ] 41 } 42 ]; 43 44const target = "B"; 45// console.log(JSON.stringify(a)); 46 47const result = a 48 .map((x) => x.custom.filter((c) => c.type === target)) 49 .filter((y) => y[0]) 50 .map((y) => y[0].id); 51 52console.log(JSON.stringify(result)); 53

追記2

該当項目が1グループに複数のときだめですね。。y[0]としているからかもですが、じゃあどうしよう。。

js

1let a = 2 // APIからのレスポンスJSON 3 [ 4 { 5 id: 1, 6 name: "甲グループ", 7 custom: [ 8 { 9 id: 1, 10 name: "甲-1", 11 type: "B" 12 } 13 ] 14 }, 15 { 16 id: 2, 17 name: "乙グループ", 18 custom: [ 19 { 20 id: 2, 21 name: "乙-1", 22 type: "A" 23 }, 24 { 25 id: 3, 26 name: "乙-2", 27 type: "B" 28 }, 29 { 30 id: 4, 31 name: "乙-3", 32 type: "B" 33 } 34 ] 35 }, 36 { 37 id: 3, 38 name: "龍グループ", 39 custom: [ 40 { 41 id: 5, 42 name: "龍-1", 43 type: "A" 44 } 45 ] 46 } 47 ]; 48 49const target = "B"; 50// console.log(JSON.stringify(a)); 51 52const result = a 53 .map((x) => x.custom.filter((c) => c.type === target)) 54 .filter((y) => y[0]) 55 .map((y) => y[0].id); 56 57console.log(JSON.stringify(result)); 58// [1,3] ([1,3,4]となるはず) 59

追記3

flat()を挟むことで配列の深さを修正することができました

js

1let a = 2 // APIからのレスポンスJSON 3 [ 4 { 5 id: 1, 6 name: "甲グループ", 7 custom: [ 8 { 9 id: 1, 10 name: "甲-1", 11 type: "B" 12 } 13 ] 14 }, 15 { 16 id: 2, 17 name: "乙グループ", 18 custom: [ 19 { 20 id: 2, 21 name: "乙-1", 22 type: "A" 23 }, 24 { 25 id: 3, 26 name: "乙-2", 27 type: "B" 28 }, 29 { 30 id: 4, 31 name: "乙-3", 32 type: "B" 33 } 34 ] 35 }, 36 { 37 id: 3, 38 name: "龍グループ", 39 custom: [ 40 { 41 id: 5, 42 name: "龍-1", 43 type: "A" 44 } 45 ] 46 } 47 ]; 48 49const target = "B"; 50// console.log(JSON.stringify(a)); 51 52const result = a 53 .map((x) => x.custom.filter((c) => c.type === target)) 54 .filter((y) => y) 55 .flat() 56 .map((y) => y.id); 57 58console.log(JSON.stringify(result)); 59

追記4

リファクタリング。flatMapを使えば1行減りました.filterも上でやっているのでnull排除できていそうなのでいらなそう。

js

1const result = a 2 .flatMap((x) => x.custom.filter((c) => c.type === target)) 3 // .filter((y) => y) 4 // .flat() 5 .map((y) => y.id);

投稿2021/06/26 06:11

編集2021/06/26 12:13
k49977

総合スコア27

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問