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

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

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

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

Q&A

解決済

3回答

412閲覧

json内の配列中のオブジェクトの特定のデータのみを取得したい

nao__funky

総合スコア62

JavaScript

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

0グッド

1クリップ

投稿2022/06/02 17:04

前提

非同期処理によりjson内に格納されている配列中のオブジェクト内から特定のデータを取得して、htmlに表示しようとしている。

実現したいこと

json内のusers内のオブジェクトからuser_nameのデータのみを取り出したい。

発生している問題・エラーメッセージ

users内のオブジェクトから全てのデータを取り出すことには成功したが、user_nameのみを取り出せない。

該当のソースコード

--js-- window.addEventListener('DOMContentLoaded', async () => { const contentRight = await fetch(statuslistData) .then((res) => res.json()) .then((x) => x); contentRight.forEach((e) => { const d = e.users; // console.log(d); d.forEach((j) => { Object.keys(j).forEach((o) => { const l = `value:${j[o]}`; console.log(l); }); }); }); // console.log(contentRight); --js-- --json-- [ { "title": "2008_○○○○○○○○○○○○○○○○○○○○○○○○○○○", "publication_date": "21/9/30", "completion_date": "21/10/30", "users": [ { "user_id": "U000001", "user_name": "松任谷 由美", "status": 0 }, { "user_id": "U000002", "user_name": "大泉 洋", "status": 0 }, { "user_id": "U000003", "user_name": "北島 三郎", "status": 0 }, { "user_id": "U000004", "user_name": "中島 みゆき", "status": 0 }, { "user_id": "U000005", "user_name": "窪田 正孝", "status": 0 } ] }, { "title": "2009_BM_CRC_補完eL①_作用○○○○○○○○○○○○○", "publication_date": "21/9/23", "completion_date": "21/10/23", "users": [ { "user_id": "U000001", "user_name": "松任谷 由美", "status": 2 }, { "user_id": "U000002", "user_name": "大泉 洋", "status": 0 }, { "user_id": "U000003", "user_name": "北島 三郎", "status": 0 }, { "user_id": "U000004", "user_name": "中島 みゆき", "status": 1 }, { "user_id": "U000005", "user_name": "窪田 正孝", "status": 1 } ] }, { "title": "2010_○_NSCLC_XXXXXXXXX○○○○○○○○○○", "publication_date": "21/9/16", "completion_date": "21/10/16", "users": [ { "user_id": "U000001", "user_name": "松任谷 由美", "status": 2 }, { "user_id": "U000002", "user_name": "大泉 洋", "status": 2 }, { "user_id": "U000003", "user_name": "北島 三郎", "status": 0 }, { "user_id": "U000004", "user_name": "中島 みゆき", "status": 2 }, { "user_id": "U000005", "user_name": "窪田 正孝", "status": 2 } ] }, { "title": "2011_K_MM_研修資料_Dkd事前○○○○○○○○", "publication_date": "21/8/20", "completion_date": "21/9/20", "users": [ { "user_id": "U000001", "user_name": "松任谷 由美", "status": 2 }, { "user_id": "U000002", "user_name": "大泉 洋", "status": 2 }, { "user_id": "U000003", "user_name": "北島 三郎", "status": 0 }, { "user_id": "U000004", "user_name": "中島 みゆき", "status": 2 }, { "user_id": "U000005", "user_name": "窪田 正孝", "status": 2 } ] }, { "title": "2011_BM_CRC_補完eL①_作用○○○○○○○○○○", "publication_date": "21/8/20", "completion_date": "21/9/20", "users": [ { "user_id": "U000001", "user_name": "松任谷 由美", "status": 2 }, { "user_id": "U000002", "user_name": "大泉 洋", "status": 2 }, { "user_id": "U000003", "user_name": "北島 三郎", "status": 0 }, { "user_id": "U000004", "user_name": "中島 みゆき", "status": 2 }, { "user_id": "U000005", "user_name": "窪田 正孝", "status": 2 } ] }, { "title": "○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○", "publication_date": "21/8/20", "completion_date": "21/12/1", "users": [ { "user_id": "U000001", "user_name": "松任谷 由美", "status": 2 }, { "user_id": "U000002", "user_name": "大泉 洋", "status": 2 }, { "user_id": "U000003", "user_name": "北島 三郎", "status": 2 }, { "user_id": "U000004", "user_name": "中島 みゆき", "status": 2 }, { "user_id": "U000005", "user_name": "窪田 正孝", "status": 2 } ] }, { "title": "○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○", "publication_date": "21/8/20", "completion_date": "21/12/1", "users": [ { "user_id": "U000001", "user_name": "松任谷 由美", "status": 1 }, { "user_id": "U000002", "user_name": "大泉 洋", "status": 1 }, { "user_id": "U000003", "user_name": "北島 三郎", "status": 0 }, { "user_id": "U000004", "user_name": "中島 みゆき", "status": 0 }, { "user_id": "U000005", "user_name": "窪田 正孝", "status": 2 } ] }, { "title": "○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○", "publication_date": "21/8/20", "completion_date": "21/12/1", "users": [ { "user_id": "U000001", "user_name": "松任谷 由美", "status": 2 }, { "user_id": "U000002", "user_name": "大泉 洋", "status": 0 }, { "user_id": "U000003", "user_name": "北島 三郎", "status": 1 }, { "user_id": "U000004", "user_name": "中島 みゆき", "status": 1 }, { "user_id": "U000005", "user_name": "窪田 正孝", "status": 1 } ] } ] --json--

試したこと

定数contentRightを定めて、forEach、Object.keysメソッドなどを用いてuser_id,user_name,statusのデータを取り出せた。必要なデータはuser_nameのみ。
const l = value:${j[o].user_name};などと行ってみたが、エラーが生じてしまう。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答3

0

ベストアンサー

javascript

1window.addEventListener('DOMContentLoaded', async()=>{ 2 const users=await fetch('sample.json').then(res=>res.json()).then(x=>x.map(x=>x.users.map(x=>x.user_name)).flat()); 3 console.log(users); 4});

投稿2022/06/03 00:25

yambejp

総合スコア114775

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

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

0

js

1const names = contentRight.map(e => e.users.map(j => j.user_name)).flat();

でしょうか。

投稿2022/06/02 21:56

編集2022/06/02 22:28
int32_t

総合スコア20841

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

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

0

j.user_nameを見ればよいと思います

投稿2022/06/02 17:25

kairi003

総合スコア1330

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問