finding the smallest number in an array javascript
const arr = [14, 58, 20, 77, 66, 82, 42, 67, 42, 4]
const min = Math.min(...arr)
console.log(min)// 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);
function removeSmallest(arr) {
var min = Math.min(...arr);
return arr.filter(e => e != min);
}Array.min = function( array ){
return Math.min.apply( Math, array );
};
var arr = [5,1,9,5,7];
var smallest = arr[0];
for(var i=1; i<arr.length; i++){
if(arr[i] < smallest){
smallest = arr[i];
}
}
console.log(smallest);
Also in JavaScript:
- javascript loop last index
- redux workflow
- angular cors issue
- how to restablished closed rxjs websocket
- electron getPath
- add webpack to react project
- adonisjs hook befor save
- convert int to float in javascript
- json in listview flutter
- how to extract strings in array js
- how to validate input type=date in javascript
- javascript datetime
- javascript print
- how to get code suggestions for react components
- javascript remove parameters from current url
- react can only work with node and express? true or false
- javascript check if is array
- jquery on hover
- toLocalString
- how to add up all the numbers in between 0 and that number
- javascript remove last element from array
- javascript loop through array
- javascript automatic click
- onclick react