max value in array javascript
// For large data, it's better to use reduce. Supose arr has a large data in this case:
const arr = [1, 5, 3, 5, 2];
const max = arr.reduce((a, b) => { return Math.max(a, b) });
// For arrays with relatively few elements you can use apply:
const max = Math.max.apply(null, arr);
// or spread operator:
const max = Math.max(...arr);
var arr = [1, 2, 3];
var max = arr.reduce(function(a, b) {
return Math.max(a, b);
});function getMaxOfArray(numArray) {
return Math.max.apply(null, numArray);
}//get min/max value of arrays
function getArrayMax(array){
return Math.max.apply(null, array);
}
function getArrayMin(array){
return Math.min.apply(null, array);
}
var ages=[11, 54, 32, 92];
var maxAge=getArrayMax(ages); //92
var minAge=getArrayMin(ages); //11Math.max(...array);const array1 = [1, 3, 2];
console.log(Math.max(...array1));
Also in JavaScript:
- collapse in angular 4
- how to check if the const is jsx
- update nodejs
- How to create HTML forms and attach JavaScript behaviors ALL TYPES
- javascript get random number
- how to write and read a text file in jdiscord.js
- jquery open page in new tab
- loop through json array and get key name
- apollo server build schema passing parameters to classes
- get last element in array in javascript
- programmatically redirect react router
- deep clone javascript object
- select option value jquery
- dom element get attribute
- js check if number has decimals
- draw point p5js
- google apps script properties service
- jquery hover
- insert json into sql
- ejs / javascript check if array/object exists and is not empty
- how to make a grocery list in javascript
- get current scroll height javascript
- how to find dates in a string in js
- reduce()