Consider the following array:
[5,2,null,5,9,4]
To replace the null
value with the average of the previous and next values (2 and 5), you can do the following:
[5,2,3.5,5,9,4]
If there are consecutive null values in an array:
[5,2,null,null,9,4]
You can replace them with step values between 2 and 9, like this:
[5,2,4.33,6.63,9,4]
Just to clarify, I'm not using this for a homework assignment but rather experimenting with the functionality offered by the Chartist-js library.
Below is a snippet that demonstrates how to replace null values with the average of neighboring elements:
var seriesData = [5,2,null,5,null,4]
seriesData.forEach(function(value, index){
if ( value === null ) {
var prev = seriesData[index - 1];
var next = seriesData[index + 1];
seriesData[index] = (prev + next) / 2;
}
});
=> [5, 2, 3.5, 5, 4.5, 4]