Max/Min value of an attribute in an array of objects in JavaScript
Last Updated :
13 Jul, 2023
In this article, we are given an array of objects and the task is to get the maximum and minimum values from the array of objects. For this approach, we have a few methods that will be discussed below.
Methods to get Min/Max values:
JavaScript apply() Method: This method is different from the function call() because of taking arguments as an array.
Syntax:
apply()
JavaScript Array map() Method: This method is used to create a new array with the results of calling a function for each element of the array. This method calls the given function once for each element in an array, in order.
Syntax:
array.map(function(cValue, index, arr), thisValue)
Example: This example gets the maximum value of the y property by using apply() and map() methods.
JavaScript
let Arr = [
{ "x": "3/10/2003", "y": 0.023452007 },
{ "x": "8/12/2002", "y": 0.02504234 },
{ "x": "1/16/2001", "y": 0.024533546 },
{ "x": "8/19/2006", "y": 0.03123423457 }];
console.log(JSON.stringify(Arr));
function gfg_Run() {
console.log("Maximum value of y = " +
Math.max.apply(Math, Arr.map(function (event) {
return event.y;
})));
}
gfg_Run();
Output[{"x":"3/10/2003","y":0.023452007},{"x":"8/12/2002","y":0.02504234},{"x":"1/16/2001","y":0.024533546},{"x":"8/19/2006","y":0.03123423457}]
Maximum value of y = 0.03123423457
This method reduces the array to a single value. This method runs a defined function for every value of the array (from left to right). The return value of the function is stored in an accumulator (result or total).
Syntax:
array.reduce(function(total, curValue, curIndex, arr), initialValue)
Example: This example gets the minimum value of the y property by using the array.reduce() method. It returns the whole object.
JavaScript
let Arr = [
{ "x": "3/10/2003", "y": 0.023452007 },
{ "x": "8/12/2002", "y": 0.02504234 },
{ "x": "1/16/2001", "y": 0.024533546 },
{ "x": "8/19/2006", "y": 0.03123423457 }];
console.log(JSON.stringify(Arr));
function gfg_Run() {
console.log(JSON.stringify(
Arr.reduce(function (prev, current) {
return (prev.y < current.y) ? prev : current
})));
}
gfg_Run();
Output[{"x":"3/10/2003","y":0.023452007},{"x":"8/12/2002","y":0.02504234},{"x":"1/16/2001","y":0.024533546},{"x":"8/19/2006","y":0.03123423457}]
{"x":"3/10/2003","y":0.023452007}
Example: In this example, we will use JavaScript for loop to get the min and max values from an array of objects
JavaScript
// Input array of objects
let Arr = [
{ "x": "3/10/2003", "y": 0.023452007 },
{ "x": "8/12/2002", "y": 0.02504234 },
{ "x": "1/16/2001", "y": 0.024533546 },
{ "x": "8/19/2006", "y": 0.03123423457 }];
// Initialize min and max values
let maxValue = Arr[0].y;
let minValue = Arr[0].y;
// Getting min and max value using for loops
for (let i = 0; i < Arr.length; i++) {
if (Arr[i].y > maxValue) {
maxValue = Arr[i].y;
}
if (Arr[i].value < minValue) {
minValue = Arr[i].y;
}
}
// Display the output
console.log(Arr)
console.log("Max Value:", maxValue);
console.log("Min Value:", minValue);
Output[
{ x: '3/10/2003', y: 0.023452007 },
{ x: '8/12/2002', y: 0.02504234 },
{ x: '1/16/2001', y: 0.024533546 },
{ x: '8/19/2006', y: 0.03123423457 }
]
Max Value: 0.03123423457
Min Value: 0.023452...
Similar Reads
How To Search The Max Value of an Attribute in an Array Object? Here are the various methods to search the max value of an attribute in an array object1. Using LoopThe array is traversed and the required values of the object are compared for each index of the array.javascriptconst prod = [ { name: 'Prod A', price: 50 }, { name: 'Prod B', price: 75 }, { name: 'Pr
4 min read
How to Select Min/Max Dates in an Array Using JavaScript? Here are the different ways to select min/max dates in an array using JavaScript1. Using Math.max and Math.minUse Math.max and Math.min function to get the maximum and minimum dates respectively.JavaScriptlet dates = [ new Date("2019/06/25"), new Date("2019/06/26"), new Date("2019/06/27"), new Date(
3 min read
How to get Values from Specific Objects an Array in JavaScript ? In JavaScript, an array is a data structure that can hold a collection of values, which can be of any data type, including numbers, strings, and objects. When an array contains objects, it is called an array of objects. Table of Content Using the forEach() methodUsing the map() methodUsing the filte
2 min read
Find the min/max element of an Array using JavaScript To find the minimum or maximum element in a JavaScript array, use Math.min or Math.max with the spread operator.JavaScript offers several methods to achieve this, each with its advantages.Using Math.min() and Math.max() Methods The Math object's Math.min() and Math.max() methods are static methods t
2 min read
How to get a key in a JavaScript object by its value ? To get a key in a JavaScript object by its value means finding the key associated with a specific value in an object. Given an object with key-value pairs, you want to identify which key corresponds to a particular value, often for searching or data retrieval.How to get a key in a JavaScript object
4 min read
How to Get Index of Greatest Value in an Array in JavaScript ? In this article, we will see how to return the index for the greatest value in an array with the help of JavaScript. To return the index of the greatest value in an array using JavaScript.Some of the examples are to return the index of the greatest value in an array.Example: The greatest value in th
4 min read
Sort an array of objects using Boolean property in JavaScript Given the JavaScript array containing Boolean values. The task is to sort the array on the basis of Boolean value with the help of JavaScript. There are two approaches that are discussed below: Table of Content Using Array.sort() Method and === OperatorUsing Array.sort() and reverse() MethodsUsing a
2 min read
JavaScript - Find Index of a Value in Array Here are some effective methods to find the array index with a value in JavaScript.Using indexOf() - Most Used indexOf() returns the first index of a specified value in an array, or -1 if the value is not found. JavaScriptconst a = [10, 20, 30, 40, 50]; // Find index of value 30 const index = a.inde
2 min read
How to Remove a Property from All Objects in an Array in JavaScript? To remove a property from all objects in an array in JavaScript, you can use the forEach() method to iterate over each object in the array and use the delete operator to remove the specified property.Example:JavaScriptconst arrayOfObjects = [ { name: "Alice", age: 25, city: "New York" }, { name: "Bo
2 min read
How to Access Array of Objects in JavaScript ? Accessing an array of objects in JavaScript is a common task that involves retrieving and manipulating data stored within each object. This is essential when working with structured data, allowing developers to easily extract, update, or process information from multiple objects within an array.How
4 min read