Reputation: 267
Is it possible to arrange the values of an array to have a range of 0.0 - 1.0. but without having to know the min and max values of the array in advance. Say I have this below.
Event:7.3339,5.20755,25343052,11.8282,-1,1,146511,-0.584812,1.41411,1.23406,-2.27636,6.23602,2.3905,2.06042,-1.55016,5.8858,-1.81976,-3.85836,4.62525
So in the future I want to make sure it will be normalized to the given range by finding the min/max values programatically.
Upvotes: 1
Views: 540
Reputation: 386604
You could take the min
and max
values and for every value subtract the min
value and divide by the delta of max
and min
.
toFixed
is only for displaying the normalised values.
function getNormalized(array) {
var min = Math.min(...array),
max = Math.max(...array);
return array.map(v => (v - min) / (max - min));
}
var array = [7.3339, 5.20755, 25343052, 11.8282, -1, 1, 146511, -0.584812, 1.41411, 1.23406, -2.27636, 6.23602, 2.3905, 2.06042, -1.55016, 5.8858, -1.81976, -3.85836, 4.62525];
console.log(getNormalized(array).map(v => v.toFixed(10)));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 1