Skip to content

Instantly share code, notes, and snippets.

@IcarusFW
Created September 11, 2018 09:49
Show Gist options
  • Save IcarusFW/c3191bf0fe31d88b879b1aa850fcb2a5 to your computer and use it in GitHub Desktop.
Save IcarusFW/c3191bf0fe31d88b879b1aa850fcb2a5 to your computer and use it in GitHub Desktop.
Find the minimum and maximum value in a series of data arrays (used with datavis plugins eg D3 to find axis ranges)
function findMinMax(arr, start) {
var min = parseInt(start[0]) || parseInt(arr[0]);
var max = parseInt(start[1]) || parseInt(arr[0]);
for (let i = 0, len=arr.length; i < len; i++) {
var v = parseInt(arr[i]);
min = (v < min) ? v : min;
max = (v > max) ? v : max;
}
return initial = [min, max];
}
var initial = [];
var data = [
["22", "20", "26", "25", "23", "22"],
["27", "28", "29", "37", "30", "27"],
["18", "22", "23", "22", "26", ""]
];
for (arrays in data) {
findMinMax(data[arrays], initial)
}
console.log(initial);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment