UNPKG

d3plus-common

Version:

Common functions and methods used across D3plus modules.

13 lines 519 B
/** @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) { var arr = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; 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; }); }