javascript - Average values based on date from json to array of arrays -
i hesitate post i've made no significant progress. should step away day. attempting average values date use in highstock charts. data in form of json. issue arises when have 2 sets of data on same day, need average these values based on date. on given day there may 0,1, or 2 values. days no values need null rather 0 there highstock show gap. have been trying work on js solution problem. conceptually seems easy; group date, sum , divide length. have not made progress. here fiddle without blunders. assistance or nudge in right direction appreciated.
{ "nfdrs": { "row": [{ "@num": "141", "sta_id": "350920", "sta_nm": "hehe 1", "latitude": "44.9559", "longitude": "-121.4991", "nfdr_dt": "08\/10\/2016", "nfdr_tm": "13", "nfdr_type": "o", "mp": "1", "msgc": "7c2p2", "one_hr": "5", "ten_hr": "6", "hu_hr": "11", "th_hr": "10", "xh_hr": "8", "ic": "28", "kbdi": "304", "sc": "8", "ec": "14", "bi": "27", "sl": "3-", "lr": "0", "lo": "0", "hr": "0", "ho": "0", "fl": "19", "hrb": "60", "wdy": "78", "adj": "m" }, { "@num": "142", "sta_id": "352108", "sta_nm": "warm springs base", "latitude": "44.7795", "longitude": "-121.2501", "nfdr_dt": "08\/10\/2016", "nfdr_tm": "13", "nfdr_type": "o", "mp": "1", "msgc": "7a2a2", "one_hr": "5", "ten_hr": "6", "hu_hr": "8", "th_hr": "8", "xh_hr": "3", "ic": "19", "kbdi": "587", "sc": "34", "ec": "2", "bi": "22", "sl": "2", "lr": "0", "lo": "0", "hr": "0", "ho": "0", "fl": "16", "hrb": "5", "wdy": "60", "adj": "l" },
and, how control size of code sample while including of code>
i guess main question "how pro grammatically perform groupby on 2 different dataset".
there many ways can this. 1 way use reduce
function merge 2 datasets 1 complete one.
example:
function printaverage(data) { (var time in data) { // ec var ec_totalvalue = 0; data[time].ec_values.foreach(value => { ec_totalvalue += value; }); var ec_average = ec_totalvalue / data[time].ec_values.length; // erc var erc_totalvalue = 0; data[time].erc_values.foreach(value => { erc_totalvalue += value; }); var erc_average = erc_totalvalue / data[time].erc_values.length; console.log("time => " + time + ", average ec => " + ec_average + ", average erc =>" + erc_average); } } function getepochtime(datestr) { return date.parse(datestr); } function hasdatafordate(dataset, epochtime) { return dataset.hasownproperty(epochtime); } function addvalue(dataset, item) { var epochkey = getepochtime(item.nfdr_dt); if (!hasdatafordate(dataset, epochkey)) { dataset[epochkey] = {}; dataset[epochkey].ec_values = []; dataset[epochkey].erc_values = []; } if (item.ec) dataset[epochkey].ec_values.push(parseint(item.ec)); if (item.erc) dataset[epochkey].erc_values.push(parseint(item.erc)); return dataset; } function grouprows(data) { return data.reduce(addvalue, {}); } var data = { "nfdrs": { "row": [ { "@num": "141", "sta_id": "350920", "sta_nm": "hehe 1", "latitude": "44.9559", "longitude": "-121.4991", "nfdr_dt": "08\/10\/2016", "nfdr_tm": "13", "nfdr_type": "o", "mp": "1", "msgc": "7c2p2", "one_hr": "5", "ten_hr": "6", "hu_hr": "11", "th_hr": "10", "xh_hr": "8", "ic": "28", "kbdi": "304", "sc": "8", "ec": "14", "erc": "80", "bi": "27", "sl": "3-", "lr": "0", "lo": "0", "hr": "0", "ho": "0", "fl": "19", "hrb": "60", "wdy": "78", "adj": "m" }, { "@num": "142", "sta_id": "352108", "sta_nm": "warm springs base", "latitude": "44.7795", "longitude": "-121.2501", "nfdr_dt": "08\/10\/2016", "nfdr_tm": "13", "nfdr_type": "o", "mp": "1", "msgc": "7a2a2", "one_hr": "5", "ten_hr": "6", "hu_hr": "8", "th_hr": "8", "xh_hr": "3", "ic": "19", "kbdi": "587", "sc": "34", "ec": "2", "erc": "100", "bi": "22", "sl": "2", "lr": "0", "lo": "0", "hr": "0", "ho": "0", "fl": "16", "hrb": "5", "wdy": "60", "adj": "l" }] } }; var grouped = grouprows(data.nfdrs.row); printaverage(grouped);
essentially code loops through data row , check if "merged dataset" has got key defined.
if has code pushes values of row value array. otherwise defines js {}
object , adds under key , pushes row value it.
for more example usages of reduce
, is.
see:
https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/array/reduce
Comments
Post a Comment