-1

I am trying to grab 4 Minimum values from a array list.

Example

var ArrayList =[2,3,4,76,23,14,99,54,21]

I would like to grab 4 Numbers so result should be 2,3,4,14

Thanks in advance.

Kevin B
  • 94,570
  • 16
  • 163
  • 180
Proweb
  • 1
  • 4

2 Answers2

0

Try This:

var array =[2,3,4,76,23,14,99,54,21];
var newarray=[];
var min;
for(i=0;i< 4;i++){
    min = Math.min.apply(Math,array); 
    newarray.push(min);
    array.splice(array.indexOf(min), 1);
}
alert(newarray);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
Tasos Fel
  • 331
  • 2
  • 6
0

first sort the list from least to greatest and splice the resulting string from the index 4 to the length of the array to get the minimum values

function myFunction() {
    arrayList.sort(function(a,b){ return (a-b)}).splice(4,arrayList.length);
}