JSONの配列データから特定の値を含むデータをselectして新しく配列を作りたい
まず
プログラミング初心者です。そのため用語など曖昧に思える点がいくつかあると思います。すみません。
プログラムの回答ができれば欲しいです。しかしここに似た記事があると言う情報だけでもありがたいです。
よろしくお願いします。
現在
授業で使う小売店のシステムを作っています。
データの取得と関数に飛ばす先の処理は完成していて、
投資番号や小売店名、商品名ごとにグループ化した配列を作っていますがうまくいきません。
発生している問題
日付(投資番号)>小売店名>商品>納期の順でフィルターをかけたいと思っているが、
日付のところしかうまくいきません。
取得するスプレッドシートのデータは以下の通りです。
(1から20までありますが、一番フィルターをかける量が多い16をピックアップしています。)
- 見積(estimate)
日付 卸社名 小売店名 商品名 納期 金額 個数 16 a x ペン 1 100 20 16 b x ペン 1 110 20 16 c x ペン 1 120 40 16 a x ペン 2 100 30 16 b x ペン 2 110 30 16 c x ペン 2 120 40 16 a x 消しゴム 1 100 20 16 b x 消しゴム 1 110 20 16 c x 消しゴム 1 120 40 16 a x 消しゴム 2 100 30 16 b x 消しゴム 2 110 30 16 c x 消しゴム 2 120 40 16 a y ペン 1 100 20 16 b y ペン 1 110 20 16 c y ペン 1 120 40 16 a y ペン 2 100 30 16 b y ペン 2 110 30 16 c y ペン 2 120 40 16 a y 消しゴム 1 100 20 16 b y 消しゴム 1 110 20 16 c y 消しゴム 1 120 40 16 a y 消しゴム 2 100 30 16 b y 消しゴム 2 110 30 16 c y 消しゴム 2 120 40
- 需要(demand)
日付 小売店名 商品名 数量 納期 16 x 消しゴム 40 1 16 x 消しゴム 40 2 16 x ペン 60 1 16 x ペン 60 2 16 y 消しゴム 40 1 16 y 消しゴム 40 2 16 y ペン 60 1 16 y ペン 60 2
飛ばす先の関数
contractDecision(estimates,demands[0].数量);
estimatesには日付、小売店、商品、納期で同じものになるようにフィルターがかけられている上で、
{卸社名、金額、個数}
が含まれていれば処理できます。
またdemands[0].数量の部分は、日付、小売店、商品、納期で同じものになるようにフィルターがかけられている上で、
需要データの数量が含まれていれば処理ができます。
試したこと
var demandTable= {} for(var i = 2; i <= testDemandSheet.getLastRow(); i++){ var date = testDemandSheet.getRange(i,1).getValue(); var product = testDemandSheet.getRange(i,3).getValue(); var store = testDemandSheet.getRange(i,2).getValue(); var quantity = testDemandSheet.getRange(i,4).getValue(); var delivdate = testDemandSheet.getRange(i,5).getValue(); if(!(date in demandTable)){ demandTable[date] = {}; } if(!(store in demandTable[date])){ demandTable[date][store] = []; } if(!(product in estimateTable[date][store])){ demandTable[date][store][product] = {}; } if(!(delivdate in estimateTable[date][store][product])){ demandTable[date][store][product][delivdate] = []; } demandTable[date][store][product][delivdate].push({"数量":quantity}); }
上記で要素ごとにJSONを組んで、みたが実行を押したら
TypeError: undefined からプロパティ「1.0」を読み取れません。
が出力された。
また
var triggerDate = 12; if(triggerDate !== null){ for(var i = 0; i <= store.length;i++){ if(estimateTable[triggerDate][store[i]] !== undefined){ productFilterEstimates = estimateTable[triggerDate][store[i]]; productFilterDemands = demandTable[triggerDate][store[i]]; Logger.log(productFilterEstimates[0]); for(var j = 0; j <= product.length; j++){ if(productFilterEstimates[product[j]] !== undefined){ storeFilterEstimates = productFilterEstimates[product[j]]; storeFilterDemands = productFilterDemands[product[j]]; Logger.log(storeFilterEstimates); contractDecision(productFilterEstimates,storeFilterDemands[0].数量); }else{ storeFilterEstimates = productFilterEstimates; storeFilterDemands = productFilterDemands; } } } } }
のように要素ごとにフィルターをかけようと思ったが、for文がたくさんになってしまい、できませんでした。
最終的には4つの要素が一致している上で、
{卸社名、金額、個数}の配列
と
需要の数量
が抜き出せたらできます!
補足情報(FW/ツールのバージョンなど)
用語なども含め、不明点が多い文章かもしれません。
ご指摘の上、ご指導お願いいたします。
あなたの回答
tips
プレビュー