javascript - How to make two arrays match by filling in zeroes? -
in javascript i'm grabbing results here, , putting them 2 variables: ticks , s1.
var viewmodel = @html.raw(json.encode(model)); var items = viewmodel.date1; var items2 = viewmodel.date2; var newcount; var ticks = []; var s1 = []; var colors = []; (var = 0; < items.length; i++) { newcount = items[i].thecount / items2[i].thecount * 100; ticks.push(items[i].thecycle); s1.push(newcount); if (newcount < 98) colors.push("#ff2f2f"); else colors.push("#00749f"); }
the results this:
how many where b.active_serv != 0 - items
cycle count 1 823 6 530 7 475 9 962 10 591 11 121 13 751 15 716 50 133 100 39
the total of them (without clause) -items2
cycle count 1 833 6 532 7 492 9 967 10 611 11 121 13 767 14 37 15 816 16 71 19 3 21 101 23 11 50 133 100 39
as see, there more cycles in total (items2) in b.active_serv != 0 (items). need fix items match items2 because percentages i'm doing:
newcount = items[i].thecount / items2[i].thecount * 100;
how go through of variable items , , add 0 in correct positions in array if cycle numbers not match (or missing).
if item array of row
objects thecycle
, thecount
members, returns array of rows in items don't have matching thecycle
member in item2
replaced {thecycle:0, thecount:0}
items = items2.map( row => //is there matching row in items? items.filter( r => r.thecycle == row.thecycle).length == 0 ? //if not, fill zeros {thecycle:0, thecount:0} : //if there is, return items' row items.filter( r => r.thecycle == row.thecycle)[0] );
of course, assumes r.thecycle
unique identifier.
for example, if had these 2 arrays:
var items = [{thecycle:2, thecount:2}, {thecycle:1, thecount:1}]; var items2 = [{thecycle:2, thecount:2}, {thecycle:1, thecount:1}, {thecycle:3, thecount:3} ];
we should obtain following resutl:
[{thecycle:2,thecount:2},{thecycle:1,thecount:1},{thecycle:0,thecount:0}]
where third nonexisting object in items
replaced zeros.
Comments
Post a Comment