Created
June 17, 2015 09:57
-
-
Save felixzapata/1f886b46109fbaa97969 to your computer and use it in GitHub Desktop.
cómo sacar el valor más cercano a un array dado uno proporcionado
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var pos = [0,20,40,60,80,100]; | |
var valor = 15; | |
var resultado = pos.map(function (val){ return {val:val, dif: Math.abs(val-valor)};}).reduce(function(acc, pair){return (acc && acc.dif < pair.dif) ? acc : pair}).val; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment