DEV Community

Melvinvmegen
Melvinvmegen

Posted on • Updated on • Originally published at blog.melvinvmegen.com

How to find the minimum value in a collection of primitives or objects with Javascript in 2023

Image description

Implementation

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;
  }, {});
}
Enter fullscreen mode Exit fullscreen mode

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() 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() 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:

// 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 }
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (4)

Collapse
 
overflow profile image
overFlow

I immediately thought Math.min().
I dont think I know of this findMin() method though.

awesome tut.

P.s I just googled and I couldnt find anything about findMin() method. Only a min() method even then vaguely still.
Would you like to elaborate please ?

Collapse
 
melvinvmegen profile image
Melvinvmegen • Edited

Indeed, it's not natively implemented is Javascript, i should've been more clear, i though the picture was enough.
FindMin() is my own implementation of what i'd like javascript to implement for this use case, the code looks like this :

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;
}, {});
}

Feel free to suggest anything you find relevant :)

Ps: I updated the blog post i hope is clearer now

Collapse
 
overflow profile image
overFlow

awesome thanks.. obviously you are more advanced than me.
I am still learning and I will learn a lot from your code by the looks of it.

Thread Thread
 
melvinvmegen profile image
Melvinvmegen

Time is of the essence, keep learning and keep enjoying you'll get there soon enought :)