Array.prototype.closest >= 0.1.2
Purpose
Get the closest numeric value inside an array
Syntax
Array#closest
(
Numbergoal
);
Parameters
goal
- The goal we want to get the closest value to
Return values
Number
Examples
Return the closest value in the array
var arr = Array.range(0, 100000, 33);
>>> [0, 33, 66, 99, ..., 99990]
arr.closest(3470);
>>> 3465
Return the first value for non-numeric goals
var arr = [0, 1, 2, 3];
arr.closest('a');
>>> 0
arr.closest(null);
>>> 0
Comments