Creating and populating an array with a fixed width in JavaScript -
i've got input array in format:
[[timestamp, jobid, time completion],[..]]
the data sql db, grouped both timestamp, , jobid, array looks like:
[ [1, 30, 400], [1, 31, 200], [2, 29, 300], .. ]
i create new array 1 column every jobid, instead of 1 row every job id, is, single row per timestamp.
so, wrote code iterated through above array, , populated new array, simple enough, except, result array isn't fixed width, is, result looks like:
[ [1, 400, 200], [2, 300] .. ]
which makes impossible me values [1] job id 30, can't have meaningful header row. is, data in format:
timestamp, jobid29, jobid30, jobid31 [ [1, 0, 400, 200], [2, 300, 0, 0], .. ]
i can't output map, unfortunately.
how can achieve this? know i'll haveto go through input once distinct jobids, , guess i'd map each jobid position, etc, i'm wondering if best way?
thank you.
my solution uses 2 arrays , 2 maps plenty of possibilities optimizations. here's rough sketch:
sort array jobid. (let's call "joblist")
keep "map" keyed on unique timestamps. ("timestampmap"). map should contain 1 entry each unique timestamp.
keep array of unique timestamps , sort it. ("timelist")
each item in "timestampmap" keeps map of jobs on timestamp value
iterate on each value in timestamp list.
within each iteration, iterate on each job in joblist. if job in corresponding timestamp map output job.completiontime
otherwise, output zero.
there 2 possible optimizations code show below. 1. use input array "joblist" without having copy it. 2. combine map of maps 1 large map.
function jobsorter(a,b) { return a.jobid - b.jobid; } function numbersort(a,b) { return - b; } function buildsortedarray(yourarray) { var index, j, item; var joblist = []; // array of {jobid, timestamp, timecomp}; var timestampmap = {}; var timestamplist = []; var key, jobkey; var output = []; // loop through every item in array (index = 0; index < yourarray.length; index++) { item = yourarray[index]; // push each item "job list" job = {jobid: item[1], timestamp: item[0], timecomp: item[2]}; joblist.push(job); // use both map , list keep track of unique timestamps key = "$timestamp$" + job.timestamp.tostring(); if (timestampmap[key] === undefined) { timestampmap[key] = {}; timestampmap[key].jobmap = {}; timestamplist.push(job.timestamp); // keep timestamp array can sort on } // add job timestamp jobkey = "$jobid$" + job.jobid.tostring(); timestampmap[key].jobmap[jobkey] = job; } // let's sorting timestamplist.sort(numbersort); // timestamplist in ascending order joblist.sort(jobsorter); // joblist in ascending order (index = 0; index < timestamplist.length; index++) { item = []; item.push(timestamplist[index]); (j = 0; j < joblist.length; j++) { key = "$timestamp$" + timestamplist[index].tostring(); jobkey = "$jobid$" + joblist[j].tostring(); if (timestampmap[key].jobmap[jobkey]) { item.push(timestampmap[key].jobmap[jobkey].timecomp); } else { item.push(0); } } output.push(item); } return output; }
Comments
Post a Comment