closest-to
closest-to is a function that, when given a target number and an array of numbers, will return the array value closest to the target.
Install
npm install closest-to
Example
var closest = require('closest-to')
closest(10, [3, 7, 1, 9, 5])
closest(-1.25, [2, 0, -1.5, -0.75])
closest(5, [4, 6])
API
closest-to(
target (number):
any number
numbers (array[number]):
the collection of numbers you
want to have searched
)
Note
As you may have noticed in the example, if two numbers are equally close to the target, the first (lowest) number is returned. It would be trivial to instead return an array, but I don’t want to add code if it’s not necessary. If this behaviour is a problem for you, please post an issue.
License
MIT