I know this is a bit on nonsense but I need to get the closest number out of 2 arrays or:
const myarr = [[12, 42], [12, 56], [30, 54]]
console.log(colsest_out_of_closest(myarr, [12, 50]))
I know this is a bit on nonsense but I need to get the closest number out of 2 arrays or:
const myarr = [[12, 42], [12, 56], [30, 54]]
console.log(colsest_out_of_closest(myarr, [12, 50]))
On
You could check the absolute delta.
const
getClosest = (a, b, t) => Math.abs(t - a) < Math.abs(t - b) ? a : b,
getClosestPair = (target, array) => values.reduce((a, b) =>
[0, 1].map(i => getClosest(a[i], b[i], target[i]))
),
values = [[12, 42], [12, 56], [30, 54]],
closest = getClosestPair([12, 50], values);
console.log(closest);
It looks like you want to find the smallest difference between both the min and the max.
Here is a reducer version that is less bytes, but still readable: