前提
非同期処理により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/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。