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

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

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

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

Q&A

解決済

2回答

258閲覧

jsonからフィルターした値をループ処理で連想配列にしたい

yakiuri

総合スコア13

JavaScript

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

0グッド

0クリップ

投稿2019/06/25 05:30

編集2019/06/25 08:18

かなり考えたのですが答えが出ずここでお力を貸していただきたく質問させていただきました。
つたない文章ですがよろしくお願いします。

前提・実現したいこと

Jsonデータから取得した値をループ処理で連想配列にしたいと考えています。
その際に時間と方角と季節をfilterして存在するものはjsonからstateの値を抜き出し、存在しないものはnoneという値を入れたいと考えています。

具体的な例

data.json内のshadowDataを direction,seasonでfilterをかける

一致したデータを個別に配列化
(方角 NEの季節 spring配列、方角 NEの季節 summer配列のように…)

配列化したデータの中身を確認、存在するデータの時間を見て、連想配列にstateを格納、存在しないデータの時間はすべてnoneにする。

{"time":"8:00", "season":"spring", "direction":"NE", "state":"partial"},
↓NEのspring には 8:00 のみ存在 
NE_spring{"8:00":"pratial","9:00":"none","10:00":"none","11:00":"none","12:00":"none","13:00":"none","14:00":"none","15:00":"none","16:00":"none" },
NE_summer{"8:00":"pratial","9:00":"none","10:00":"none","11:00":"none","12:00":"none","13:00":"none","14:00":"none","15:00":"none","16:00":"none" }.....以下略

(作成した連想配列をループ処理で参照してCSVで列として書き出し)

というプログラムを考えておりましたが自分の知識不足でうまく配列化、データの取り出しができずCSVを書き出した際undefinedになってしまいまっています。

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

季節と方角毎にフィルターをかけるところまでは成功しましたがそこから先がわかりません…

エラーメッセージ

該当のソースコード

JavaScript

1 let i = 0; 2 let n = 0; 3 let b = 0; 4 let replaceTime = []; 5 let replaceDirection = []; 6 let getShadowData = []; 7 let replaceState = []; 8 let replaceText = ''; 9 10 for(let i in timeData) { 11 replaceTime[i] = 'T' + timeData[i].replace(/:00/g, ''); 12 } 13 var csv = '\ufeff' + '&shadowData=DIR_SEASON' + ',' + replaceTime +'\n' 14 15 for(let i in directionData) { 16 replaceDirection[i] = directionData[i].direction 17 getShadowData = shadowData.filter(x => x.direction == directionData[i].direction) 18 for(let n in seasonData) { 19 replaceText = replaceDirection[i] + '_' + seasonData[n].season; 20 replaceState.push(getShadowData.filter(x => x.season == seasonData[n].season)); 21 } 22 } 23 directionData.forEach(el => { 24 replaceDirection[i] = this.directionData[i].direction 25 for(let n in this.seasonData) { 26 replaceText = replaceDirection[i] + '_' + this.seasonData[n].season 27 for (let b in replaceText) { 28 var line = '\ufeff' + replaceText + ',' + replaceState[b].state +'\n' 29 } 30 csv += line 31 } 32 i++ 33 }) 34 35 let blob = new Blob([csv], { type: 'text/csv' }); 36 let link = document.createElement('a') 37 link.href = window.URL.createObjectURL(blob) 38 link.download = 'shadowData.csv' 39 link.click()

data.json

1 "timeData": [ 2 "8:00","9:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00" 3 ], 4 "seasonData": [ 5 {"season":"spring","text":"春秋分"}, 6 {"season":"summer","text":"夏至"}, 7 {"season":"winter","text":"冬至"} 8 ], 9 "directionData": [ 10 {"direction":"NE","text":"北東"}, 11 {"direction":"NW","text":"北西"}, 12 {"direction":"SE","text":"南東"}, 13 {"direction":"SW","text":"南西"} 14 ], 15 "shadowData": [ 16 {"time":"8:00", "season":"spring", "direction":"NE", "state":"partial"}, 17 {"time":"8:00", "season":"summer", "direction":"NE", "state":"partial"}, 18 19 {"time":"15:00", "season":"spring", "direction":"NW", "state":"partial"}, 20 {"time":"16:00", "season":"spring", "direction":"NW", "state":"partial"}, 21 {"time":"13:00", "season":"summer", "direction":"NW", "state":"partial"}, 22 {"time":"14:00", "season":"summer", "direction":"NW", "state":"partial"}, 23 {"time":"15:00", "season":"summer", "direction":"NW", "state":"partial"}, 24 {"time":"16:00", "season":"summer", "direction":"NW", "state":"partial"}, 25 {"time":"15:00", "season":"winter", "direction":"NW", "state":"partial"}, 26 {"time":"16:00", "season":"winter", "direction":"NW", "state":"partial"}, 27 28 {"time":"8:00", "season":"spring", "direction":"SE", "state":"partial"}, 29 {"time":"9:00", "season":"spring", "direction":"SE", "state":"partial"}, 30 {"time":"10:00", "season":"spring", "direction":"SE", "state":"partial"}, 31 {"time":"11:00", "season":"spring", "direction":"SE", "state":"partial"}, 32 {"time":"12:00", "season":"spring", "direction":"SE", "state":"partial"}, 33 {"time":"13:00", "season":"spring", "direction":"SE", "state":"partial"}, 34 {"time":"8:00", "season":"summer", "direction":"SE", "state":"partial"}, 35 {"time":"9:00", "season":"summer", "direction":"SE", "state":"partial"}, 36 {"time":"10:00", "season":"summer", "direction":"SE", "state":"partial"}, 37 {"time":"11:00", "season":"summer", "direction":"SE", "state":"partial"}, 38 {"time":"12:00", "season":"summer", "direction":"SE", "state":"partial"}, 39 {"time":"8:00", "season":"winter", "direction":"SE", "state":"partial"}, 40 {"time":"9:00", "season":"winter", "direction":"SE", "state":"partial"}, 41 {"time":"10:00", "season":"winter", "direction":"SE", "state":"partial"}, 42 {"time":"11:00", "season":"winter", "direction":"SE", "state":"partial"}, 43 {"time":"12:00", "season":"winter", "direction":"SE", "state":"partial"}, 44 {"time":"13:00", "season":"winter", "direction":"SE", "state":"partial"}, 45 {"time":"14:00", "season":"winter", "direction":"SE", "state":"partial"}, 46 47 {"time":"10:00", "season":"spring", "direction":"SW", "state":"partial"}, 48 {"time":"11:00", "season":"spring", "direction":"SW", "state":"partial"}, 49 {"time":"12:00", "season":"spring", "direction":"SW", "state":"partial"}, 50 {"time":"13:00", "season":"spring", "direction":"SW", "state":"partial"}, 51 {"time":"14:00", "season":"spring", "direction":"SW", "state":"partial"}, 52 {"time":"15:00", "season":"spring", "direction":"SW", "state":"partial"}, 53 {"time":"16:00", "season":"spring", "direction":"SW", "state":"partial"}, 54 {"time":"12:00", "season":"summer", "direction":"SW", "state":"partial"}, 55 {"time":"13:00", "season":"summer", "direction":"SW", "state":"partial"}, 56 {"time":"14:00", "season":"summer", "direction":"SW", "state":"partial"}, 57 {"time":"15:00", "season":"summer", "direction":"SW", "state":"partial"}, 58 {"time":"16:00", "season":"summer", "direction":"SW", "state":"partial"}, 59 {"time":"8:00", "season":"winter", "direction":"SW", "state":"partial"}, 60 {"time":"9:00", "season":"winter", "direction":"SW", "state":"partial"}, 61 {"time":"10:00", "season":"winter", "direction":"SW", "state":"partial"}, 62 {"time":"11:00", "season":"winter", "direction":"SW", "state":"partial"}, 63 {"time":"12:00", "season":"winter", "direction":"SW", "state":"partial"}, 64 {"time":"13:00", "season":"winter", "direction":"SW", "state":"partial"}, 65 {"time":"14:00", "season":"winter", "direction":"SW", "state":"partial"}, 66 {"time":"15:00", "season":"winter", "direction":"SW", "state":"partial"}, 67 {"time":"16:00", "season":"winter", "direction":"SW", "state":"partial"} 68

試したこと

ifによる条件分岐 → undefinedになってしまいエラーがでてしまう。

###最終的にどうなってほしいのか
以下のような形式のCSVファイルでの書き出しを行いたい
■■■■■ t8  t9 t10 t11 t12 t13 t14 t15 t16
NE_spring prital, shadow, shadow, shadow, shadow, shadow, shadow, shadow, shadow
NE_summer prital, shadow, shadow, shadow, shadow, shadow, shadow, shadow, shadow
NE_winter shadow, shadow, shadow, shadow, shadow, shadow, shadow, shadow, shadow

以下略

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

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

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

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

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

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

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

gentaro

2019/06/25 05:34

頑張って読めばわかるのかもしれませんが、ちょっと直感的に何がやりたいのかわかりにくいので、できれば「期待する結果」を明示していただきたいです。
yakiuri

2019/06/25 08:39

失礼しました。 やりたいことを最後まで書いてみました。わかりにくければ補足させていただきますのでよろしくお願い致します。
guest

回答2

0

細かいところで不明な仕様がありますが、ざっくりこんな感じ?

javascript

1var a=["8:00","9:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00" ]; 2var b1=[ 3 {"time":"8:00", "season":"spring", "direction":"NE", "state":"partial"}, 4 ]; 5var c1={}; 6a.forEach(x=>{ 7 var z=b1.filter(y=>y.time==x); 8 c1[x]=((z.length>0 && x==z[0].time)?z[0].state:"none"); 9}); 10var b2=[ 11 {"time":"15:00", "season":"spring", "direction":"NW", "state":"shadow"}, 12 {"time":"16:00", "season":"spring", "direction":"NW", "state":"partial"}, 13 ]; 14var c2={}; 15a.forEach(x=>{ 16 var z=b2.filter(y=>y.time==x); 17 c2[x]=((z.length>0 && x==z[0].time)?z[0].state:"none"); 18}); 19console.log(c1); 20console.log(c2);

投稿2019/06/25 05:52

yambejp

総合スコア114814

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

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

0

ベストアンサー

こんな感じですかね。
サンプルコード

js

1 2const timeData = [ 3 "8:00", "9:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00" 4] 5const seasonData = [ 6 { "season": "spring", "text": "春秋分" }, 7 { "season": "summer", "text": "夏至" }, 8 { "season": "winter", "text": "冬至" } 9] 10const directionData = [ 11 { "direction": "NE", "text": "北東" }, 12 { "direction": "NW", "text": "北西" }, 13 { "direction": "SE", "text": "南東" }, 14 { "direction": "SW", "text": "南西" } 15] 16const shadowData = [ 17 { "time": "8:00", "season": "spring", "direction": "NE", "state": "partial" }, 18 { "time": "8:00", "season": "summer", "direction": "NE", "state": "partial" }, 19 { "time": "15:00", "season": "spring", "direction": "NW", "state": "partial" }, 20 { "time": "16:00", "season": "spring", "direction": "NW", "state": "partial" }, 21 { "time": "13:00", "season": "summer", "direction": "NW", "state": "partial" }, 22 { "time": "14:00", "season": "summer", "direction": "NW", "state": "partial" }, 23 { "time": "15:00", "season": "summer", "direction": "NW", "state": "partial" }, 24 { "time": "16:00", "season": "summer", "direction": "NW", "state": "partial" }, 25 { "time": "15:00", "season": "winter", "direction": "NW", "state": "partial" }, 26 { "time": "16:00", "season": "winter", "direction": "NW", "state": "partial" }, 27 { "time": "8:00", "season": "spring", "direction": "SE", "state": "partial" }, 28 { "time": "9:00", "season": "spring", "direction": "SE", "state": "partial" }, 29 { "time": "10:00", "season": "spring", "direction": "SE", "state": "partial" }, 30 { "time": "11:00", "season": "spring", "direction": "SE", "state": "partial" }, 31 { "time": "12:00", "season": "spring", "direction": "SE", "state": "partial" }, 32 { "time": "13:00", "season": "spring", "direction": "SE", "state": "partial" }, 33 { "time": "8:00", "season": "summer", "direction": "SE", "state": "partial" }, 34 { "time": "9:00", "season": "summer", "direction": "SE", "state": "partial" }, 35 { "time": "10:00", "season": "summer", "direction": "SE", "state": "partial" }, 36 { "time": "11:00", "season": "summer", "direction": "SE", "state": "partial" }, 37 { "time": "12:00", "season": "summer", "direction": "SE", "state": "partial" }, 38 { "time": "8:00", "season": "winter", "direction": "SE", "state": "partial" }, 39 { "time": "9:00", "season": "winter", "direction": "SE", "state": "partial" }, 40 { "time": "10:00", "season": "winter", "direction": "SE", "state": "partial" }, 41 { "time": "11:00", "season": "winter", "direction": "SE", "state": "partial" }, 42 { "time": "12:00", "season": "winter", "direction": "SE", "state": "partial" }, 43 { "time": "13:00", "season": "winter", "direction": "SE", "state": "partial" }, 44 { "time": "14:00", "season": "winter", "direction": "SE", "state": "partial" }, 45 { "time": "10:00", "season": "spring", "direction": "SW", "state": "partial" }, 46 { "time": "11:00", "season": "spring", "direction": "SW", "state": "partial" }, 47 { "time": "12:00", "season": "spring", "direction": "SW", "state": "partial" }, 48 { "time": "13:00", "season": "spring", "direction": "SW", "state": "partial" }, 49 { "time": "14:00", "season": "spring", "direction": "SW", "state": "partial" }, 50 { "time": "15:00", "season": "spring", "direction": "SW", "state": "partial" }, 51 { "time": "16:00", "season": "spring", "direction": "SW", "state": "partial" }, 52 { "time": "12:00", "season": "summer", "direction": "SW", "state": "partial" }, 53 { "time": "13:00", "season": "summer", "direction": "SW", "state": "partial" }, 54 { "time": "14:00", "season": "summer", "direction": "SW", "state": "partial" }, 55 { "time": "15:00", "season": "summer", "direction": "SW", "state": "partial" }, 56 { "time": "16:00", "season": "summer", "direction": "SW", "state": "partial" }, 57 { "time": "8:00", "season": "winter", "direction": "SW", "state": "partial" }, 58 { "time": "9:00", "season": "winter", "direction": "SW", "state": "partial" }, 59 { "time": "10:00", "season": "winter", "direction": "SW", "state": "partial" }, 60 { "time": "11:00", "season": "winter", "direction": "SW", "state": "partial" }, 61 { "time": "12:00", "season": "winter", "direction": "SW", "state": "partial" }, 62 { "time": "13:00", "season": "winter", "direction": "SW", "state": "partial" }, 63 { "time": "14:00", "season": "winter", "direction": "SW", "state": "partial" }, 64 { "time": "15:00", "season": "winter", "direction": "SW", "state": "partial" }, 65 { "time": "16:00", "season": "winter", "direction": "SW", "state": "partial" }] 66 67const flatData = directionData.map((dir) => { 68 return seasonData.map(season => { 69 return { 70 'dirction': dir.direction, 71 'season': season.season, 72 'shadow': shadowData.filter(shadow => shadow.direction === dir.direction && shadow.season === season.season) 73 } 74 }) 75 } 76).reduce((acc, val) => acc.concat(val),[]) 77 78const result = flatData.map((d) => { 79 const r = {} 80 timeData.forEach(time => { 81 r[time] = d.shadow.find(_d => _d.time === time) ? d.shadow.find(_d => _d.time === time).state : 'none' 82 }) 83 return { 84 [d.dirction + '_' + d.season]: r 85 } 86}) 87 88console.log(result)

投稿2019/07/02 14:00

編集2019/07/02 14:52
KuwabataK

総合スコア306

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

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

yakiuri

2019/07/11 06:20

理想通りの形にすることができました、ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問