I have script for get three values (minimal numbers) from array :
$(document).ready(function(){
var _array = [1,2,3,4];
var save = 0;
var _new_array = [];
var nearDistance = [];
$.each(new Array(3),function(n){
if (_new_array.length < 1){
save = Math.min.apply(Math,_array);
_new_array = jQuery.grep(_array, function(value) {
return value != save;
});
nearDistance.push(save);
} else {
save = Math.min.apply(Math,_new_array);
_new_array = jQuery.grep(_new_array, function(value) {
return value != save;
});
nearDistance.push(save);
}
});
console.log(nearDistance);
});
This script works to get 3 minimal values,
scenario script : Do each 3 times, first each get minimal value (save
) from first array (_array
), delete minimal value from the first array (_array
) and then put resudial values to new array (_new_array
), minimal value push to nearDistance
, next each get minimal value from new array.
I think my script too less performance if I want to get more values from an array, what do you think about my script?