I have an array of values. (cols
)
I want to find out which index of cols
contains the value closest to the mean of cols
.
Can I combine these functions so that I'm not repeating loops?
let mean = cols.reduce((prev, curr) => prev + curr) / cols.length;
let closest = cols.reduce((prev, curr) => (curr - mean) < (prev - mean) ? curr : prev);
let meanCol = cols.indexOf(closest);