Get multiple minimal number from array 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 &lt; 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?
If I understand your question correctly, you're trying to:


Get n smallest numbers from your array.
The original array doesn't get mutated.


First off:


Underscores have a special meaning in programming. They're used to denote pseudo-private variables. Since JS has no concept of private (unless emulated with closures), prefixing an underscore to a publicly accessible variable means not to access that variable directly.
For this operation, you don't need jQuery. Vanilla JS has it's own forEach for $.each and filter for $.grep.
Just so you know, $ and jQuery are the same. The $ is an alias made for convenience. jQuery is the global of jQuery to prevent issue with clobbering. IIRC, MooTools uses the $ global. Anyone can alias their library as $, but who names their library "jQuery" besides jQuery?
$(document).ready(fn) can be shortened to $(fn) or jQuery(fn).
This operation is best moved out to a function so that you can call this out, pass in an array, and get the results you want.
Another problem is that $.grep return a new array every call. That means on each iteration of $.each, you're creating a new array - which is bad.
Math.min in your code will always return the smallest number. This means on every push, the numbers run from smallest to largest. Why not sort the array first and slice the first n?


Running with the above assumptions and review, you can do this:



function getSmallestNumbers(array, n) {
  // We slice first to prevent mutation of the original array when we
  // do `sort`. Then we slice again to get n items.
  return array.slice().sort().slice(0, n);
}

var distances = [4, 3, 1, 2];
var nearDistances = getSmallestNumbers(distances, 3);

// SE should seriously have a console view
document.write(JSON.stringify(nearDistances));