I have a javascript array like this
var array1= [10,20,30,40,50];
Is there any method using which i can get the closest array element to a given number ? Ex: if i pass 26, It should return 30 ( 26 is closest to 30). If i pass 42, It should return 40.
Any thoughts ? Should i iterate thru each elements ? Is there any methods available for this in jQuery ?
Simple with a for loop. No jQuery magic necessary: