let floats = [6, 10, 10, 10, 20, 48, 50.5, 60, 60, 78, 90];
let evens = [{}];
for(let i = 0; i < floats.length; i++){
if(floats[i] == floats[i - 1]) { evens[evens.length - 1][i - 1] = floats[i - 1], evens[evens.length - 1][i] = floats[i]; }
else { evens.push({}); }
}
evens = evens.filter(value => JSON.stringify(value) !== '{}');
evens.forEach(d_ =>{
let indices = Object.keys(d_);
let offset = 1;
let predecessor = Number(indices[0]) - 1;
let successor = Number(indices[indices.length - 1]) + 1;
if(predecessor < 0) { predecessor = 0; offset = 2; }
if(successor > floats.length - 1) { successor = floats.length - 1; offset = 0; }
let steps = (floats[successor] - floats[predecessor]) / (indices.length + offset);
indices.forEach((d2_, i_) => { floats[d2_] = floats[predecessor] + steps * (i_ + 1); })
})
console.log(floats);
If I have an array of floating point numbers like this:
var floats = [6.0, 10.1, 10.1, 10.1, 20.0, 32.0, 41.0, 50.5, 60.0, 60.0, 78.0];
What is the best way to adjust it so that a sequence of even numbers, say [10.1, 60.0], interpolated between their predecessors and successors?
[6.0, 9.5, 13.0, 16.5, 20.0, 32.0, 41.0, 50.5, 59.6666, 68.8333, 78.0]
10.1, 10.1, 10.1 > 9.5, 13.0, 16.5
60.0, 60.0 > 59.6666, 68.8333