EN
JavaScript - find min value in array with Array reduce method
7
points
In this article, we would like to show you how to find the minimum value in an array working with JavaScript.
1. Array
reduce
method example (ES6+)
// ONLINE-RUNNER:browser;
const array = [3, 1, 2];
const result = array.reduce((a, b) => Math.min(a, b));
console.log(result); // 1
With nested properties:
// ONLINE-RUNNER:browser;
const array = [{value: 1}, {value: 2}, {value: 3}];
const result = array.reduce((r, b) => Math.min(r, b.value), Number.POSITIVE_INFINITY);
console.log(result);
2. ES5 example
In ECMAScript 2009 we need to use classic function notation.
// ONLINE-RUNNER:browser;
var array = [1, 2, 3];
var result = array.reduce(function(a, b) {
return Math.max(a, b);
});
console.log(result);