Skip to content

Instantly share code, notes, and snippets.

@felixzapata
Created June 17, 2015 09:57
Show Gist options
  • Save felixzapata/1f886b46109fbaa97969 to your computer and use it in GitHub Desktop.
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
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