Jovica Aleksic
Jul 30, 2017 · 1 min read

You could also get the job done with just one call instead of two, using reduce.

Some ideas:

function getMinMax(data) {
return data.reduce((result, obj) => {
if (obj.y < result[0]) result[0] = obj.y;
if (obj.y > result[1]) result[1] = obj.y;
return result;
}, [Number.MAX_VALUE, Number.MIN_VALUE])
}
const [minY, maxY] = getMinMax(data);
function getMinMaxObj(data, prop) {
return data.reduce((result, obj) => {
if (obj[prop] < result.min) result.min = obj[prop];
if (obj[prop] > result.max) result.max = obj[prop];
return result;
}, {min: Number.MAX_VALUE, max: Number.MIN_VALUE})
}
const {min, max} = getMinMaxObj(data, 'y');
/*
function getMinMaxObj(data, prop) {
return data.reduce((result, obj) => {
return {
min: Math.min(obj[prop], result.min),
max: Math.max(obj[prop], result.max),
};
}, {min: Number.MAX_VALUE, max: Number.MIN_VALUE})
}
*/

Regards
Jovi

    Jovica Aleksic

    Written by

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade