Search code snippets, questions, articles...

Get Top MAX and MIN itens inside array Javascript

var maxPoints = [];
var scoreByPattern = [93,17,56,91,98,33,9,38,55,78,29,81,60];

function cloneArray(array) {
    return array.map(function(i){ return i; });
}    
function max3(array) {
    return cloneArray(array).sort(function(a,b) { return b-a; }).slice(0,3);
}
function min3(array) {
     return cloneArray(array).sort(function(a,b) { return a-b; }).slice(0,3);
}

var array=scoreByPattern;
alert("Max:"+ max3(array)[0] +' '+max3(array)[1] +' '+max3(array)[2]);
alert("Min:"+ min3(array)[0] +' '+min3(array)[1] +' '+min3(array)[2]);
// Max:98 93 91
// Min:9 17 29

//Top 5 itens
scoreByPattern.sort((a, b) => b - a).slice(0, 5);
// [98, 93, 91, 81, 78]

Get Top MAX and MIN itens inside array Javascript 

Search Index Data (The code snippet can also be found with below search text)

Get Top MAX and MIN itens inside array Javascript
get top max and min itens inside array javascript
Was this helpful?
0 Comments