How to find the minimum value in a collection of primitives or objects with Javascript in 2023
Tags: javascript
Published: 2023-07-30 10:51:16

---
title: How to find the minimum value in a collection of primitives or objects with Javascript in 2023
published: true
tags: Javascript
canonical_url: https://blog.melvinvmegen.com/snippets/array-min-value
---

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/43d37146y9zub6sh75q3.png)

## Implementation
```js
function findMin(data, key) {
  return data.reduce((accumulator, currentValue) => {
    const computedAccumulator = key ? accumulator[key] : accumulator;
    const computedCurrentValue = key ? currentValue[key] : currentValue;
    return computedAccumulator <= computedCurrentValue
      ? accumulator
      : currentValue;
  }, {});
}
```

## Context

As a JavaScript developer doing some data analysis is frequent even more so when the task at hand is about finding the minimum or even the maximum value of a collection. The code snippet above is an implementation of a function that finds the **minimum value** in a collection of objects but it could be easily extended to support other comparator.

## Usage

The function **findMin()** takes in two parameters **data** and **key** where data could be an array of primitive or objects. In case of objects, the **key** parameter becomes essential as it allows to specify which property to use for comparison. This function could be described in 2 simple steps:

- First, the [reduce()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce) method is used to iterate over the array of values and reduce it to a single value, here, **the minimum value found**. In case of an object, the returned value is the object with the minimum value of the **specified key parameter**.
- Second, inside the [reduce()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce) method, we compare the **current value** with the **previous smallest value** (starting with an empty object). If the **current value** has a lower value than the **previous smallest value**, it is replace by the **current value**.

**Note:** In case of an object **the key must be provided** so we first checks if it is. If so, we use the specified property for comparison. Otherwise, we compare the entire value (in case of a object without the key parameter it will compare pointers which leads to random results).

Now, let's make this more concrete:

```js
// Array of primitives
const randomNumbers = [124, "42", 99, 11];
findMin(randomNumbers); // 11

// Array of objects
const products = [
  { id: 1, name: "Beef", price: 10 },
  { id: 2, name: "Salmon", price: 20 },
  { id: 3, name: "Chicken", price: 5 },
];
findMin(products, "price"); 
// And tada 🎉
// { id: 3, name: "Chicken", price: 5 }
```

**Note:** we could easily change our function to handle other comparators, like finding the maximum value (>=) or the value that meets a certain condition.

## Conclusion
The **findMin()** function is a useful snippet for finding the minimum value in a collection of primitives or objects with a provided **key**. With its ability to compare objects by a specified property, it can be used in various scenarios to find the smallest value of a particular property in a collection and could be extended to use a custom comparator.