d3plus-common
Version:
Common functions and methods used across D3plus modules.
14 lines (12 loc) • 509 B
JavaScript
/**
@function closest
@desc Finds the closest numeric value in an array.
@param {Number} n The number value to use when searching the array.
@param {Array} arr The array of values to test against.
*/
export default function(n, arr) {
if ( arr === void 0 ) arr = [];
if (!arr || !(arr instanceof Array) || !arr.length) { return undefined; }
return arr.reduce(function (prev, curr) { return Math.abs(curr - n) < Math.abs(prev - n) ? curr : prev; });
}
//# sourceMappingURL=closest.js.map