I get an Array with an unknown Number of data. But I only have an predefined amount of data to be shown/store. How can I take every nth Element of the initial Array and reduce it in JavaScript?
Eg.: I get an Array with size=10000, but are only able to show n=2k Elements.
I tried it like that: delta= Math.round(10*n/size)/10 = 0.2 -> take every 5th Element of the initial Array.
for (i = 0; i < oldArr.length; i++) {
arr[i] = oldArr[i].filter(function (value, index, ar) {
if (index % delta != 0) return false;
return true;
});
}
With 0.2 it´s always 0, but with some other deltas (0.3) it is working. Same for delta=0.4, i works, but every second Element is taken with that. What can I do to get this to work?
n? What isk? What isdelta? What isoldArr?delta = size / n?someInt % 0.2 == 0always. I think you wantsomeInt % (1 / 0.2), iesomeInt % 51 % 0.2produces0.19999999999999996for me, because floating point math is broken1 % 0.25works (because 0.25 can be exactly represented in binary, I suppose), I guess using modulus operator on non-integer operands is a bad idea in JS.