Languages
[Edit]
EN

JavaScript - find min value in array with Array reduce method

7 points
Created by:
Wiktor-Sribiew
800

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);

 

See also

  1. JavaScript - Math.min() method example

Alternative titles

  1. JavaScript - find minimum value in array with Array reduce method
  2. JavaScript - find smallest value in array with Array reduce method
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join