Find maximum and minimum object among the array of objects in JavaScript
This post will discuss how to find the maximum and minimum object among the array of objects in JavaScript.
To find the maximum and minimum of custom objects, we need to define a criterion for comparing them, such as a specific property or a custom function. Here are some of the methods we can use:
1. Using Math.max() and Math.min() functions with apply() function
To find the maximum and minimum object among the array of objects, we need to compare the values of a specific property of the objects. There are different ways to do this, but one common function is to use the Math.max() and Math.min() functions, which return the maximum and minimum values among a list of arguments. However, these functions only work with numbers, not objects. To use them with objects, we need to use the apply() function, which allows us to call a function with a given this value and an array of arguments. We also need to use the map() function, which creates a new array with the results of calling a function on every element in an array. For instance:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
// Create an array of custom objects var people = [ {name: "Joseph", age: 25, height: 150}, {name: "Anne", age: 30, height: 170}, {name: "Christopher", age: 35, height: 200} ]; // Define a function that returns the age property of an object function getAge(obj) { return obj.age; } // Use Math.max() and Math.min() with apply() and map() to find the maximum and minimum age var maxAge = Math.max.apply(null, people.map(getAge)); // 35 var minAge = Math.min.apply(null, people.map(getAge)); // 25 // Min age: 25, Max age: 35 console.log(`Min age: ${minAge}, Max age: ${maxAge}`); // Alternatively, we can use an anonymous function instead of defining getAge var maxHeight = Math.max.apply(null, people.map(function(obj) { return obj.height; // 200 })); var minHeight = Math.min.apply(null, people.map(function(obj) { return obj.height; // 150 })); // Min height: 150, Max height: 200 console.log(`Min height: ${minHeight}, Max height: ${maxHeight}`); |
2. Using Array.reduce() function
The Array.reduce() function executes a reducer function on each element of an array, resulting in a single output value. The reducer function takes four arguments: accumulator, current value, current index, and source array. The accumulator is the value that accumulates the result of the reducer function. The current value is the current element being processed in the array. The current index is the index of the current element in the array. The source array is the array that reduce() was called upon. We can use this function to compare each element in an array of objects and return the maximum or minimum object according to a criterion. For instance:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
// Create an array of custom objects var people = [ {name: "Joseph", age: 25, height: 150}, {name: "Anne", age: 30, height: 170}, {name: "Christopher", age: 35, height: 200} ]; // Define a reducer function that returns the object with the maximum age function maxAgeReducer(accumulator, currentValue) { // If the accumulator is null or undefined, return the current value if (accumulator === null) { return currentValue; } // Otherwise, compare the age properties and return the object with the maximum age else { return accumulator.age > currentValue.age ? accumulator : currentValue; } } // Use reduce with maxAgeReducer to find the object with the maximum age var maxAgeObj = people.reduce(maxAgeReducer); // {name: "Christopher", age: 35, height: 200} console.log(maxAgeObj); // Define a reducer function that returns the object with the minimum height function minHeightReducer(accumulator, currentValue) { // If the accumulator is null or undefined, return the current value if (accumulator === null) { return currentValue; } // Otherwise, compare the height properties and return the object with the minimum height else { return accumulator.height < currentValue.height ? accumulator : currentValue; } } // Use reduce with minHeightReducer to find the object with the minimum height var minHeightObj = people.reduce(minHeightReducer); // {name: "Joseph", age: 25, height: 150} console.log(minHeightObj); |
3. Using Lodash or Underscore.js library
This is a more convenient way to find the maximum and minimum of custom objects in JavaScript. Lodash and Underscore.js are popular libraries that provide various utility functions for working with arrays, objects, collections, and other data types. To use these libraries, we need to include them in our HTML file or import them in our JavaScript file. For instance:
To find the maximum and minimum of custom objects using Lodash or Underscore.js, we can use their maxBy() and minBy() functions. These functions take an array of objects and a criterion as arguments, and return the object with the maximum or minimum value of the criterion. The criterion can be a property name, a function, or an array of property names. For instance:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// Import Lodash in our JavaScript file const _ = require("lodash"); // Create an array of custom objects var people = [ {name: "Joseph", age: 25, height: 150}, {name: "Anne", age: 30, height: 170}, {name: "Christopher", age: 35, height: 200} ]; // 1. Use maxBy() and minBy() with a property name to find the object // with the maximum or minimum value of that property var maxAgeObj = _.maxBy(people, "age"); console.log(maxAgeObj); // {name: "Christopher", age: 35, height: 200} var minHeightObj = _.minBy(people, "height"); console.log(minHeightObj); // {name: "Joseph", age: 25, height: 150} // 2. Use maxBy() and minBy() with a function to find the object // with the maximum or minimum value of the function result var maxNameLengthObj = _.maxBy(people, function(obj) { return obj.name.length; }); console.log(maxNameLengthObj); // {name: "Christopher", age: 35, height: 200} var minNameLengthObj = _.minBy(people, function(obj) { return obj.name.length; }); console.log(minNameLengthObj); // {name: "Anne", age: 30, height: 170} |
We can find more information and examples about these functions in this web page for Lodash and this web page for Underscore.js.
That’s all about finding the maximum and minimum object among the array of objects in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)