javascript
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
Was this helpful?
Similar Posts
- Assign min and max values to y-axis in Chart.js
- Finding the max/min value of an attribute in an array of objects
- Parenthesis Validation, Max Depth and Max Breadth - Braze - Full Stack - 12/20/21
- Get total sum of specif attribute inside of array of objects
- Find the index of Max value in an array using Javascript
- Bind external value inside .map javascript
- Javascript Code for Sorting array and finding Largest No. in Array