UNPKG

509 BJavaScriptView Raw
1/**
2 @function closest
3 @desc Finds the closest numeric value in an array.
4 @param {Number} n The number value to use when searching the array.
5 @param {Array} arr The array of values to test against.
6*/
7export default function(n, arr) {
8 if ( arr === void 0 ) arr = [];
9
10 if (!arr || !(arr instanceof Array) || !arr.length) { return undefined; }
11 return arr.reduce(function (prev, curr) { return Math.abs(curr - n) < Math.abs(prev - n) ? curr : prev; });
12}
13
14//# sourceMappingURL=closest.js.map
\No newline at end of file