Open In App

Max/Min value of an attribute in an array of objects in JavaScript

Last Updated : 13 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Method 1: Using JavaScript apply() and Array map()

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


Method 2: Using JavaScript Array reduce() Method

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}


Method 3: Using JavaScript loops

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

Extract unique objects by attribute from array of objects
Given an array of objects and the task is to return the unique object by the attribute. Examples: Input: [ { name: 'Geeks', id: 10 }, { name: 'GeeksForGeeks', id: 10 }, { name: 'Geeks', id: 20 }, { name: 'Geeks', id: 10 } ]Output:[ { name: 'Geeks', id: 10 }, { name: 'GeeksForGeeks', id: 10 } ]Approach: Let's assume that name is an attribute that di
4 min read
How to Convert Array of Objects into Unique Array of Objects in JavaScript ?
Arrays of objects are a common data structure in JavaScript, often used to store and manipulate collections of related data. However, there are scenarios where you may need to convert an array of objects into a unique array, removing any duplicate objects based on specific criteria. JavaScript has various methods to convert an array of objects into
5 min read
Filter Array of Objects with Another Array of Objects in JavaScript
Filtering an array of objects with another array in JavaScript involves comparing and including objects based on specific criteria. Below are the approaches to filter an array of objects with another array of objects in JavaScript: Table of Content Using filter and includes MethodsUsing LoopingUsing filter and includes MethodsIn this approach, we a
2 min read
How to Separate Array of Objects into Multiple Objects in JavaScript ?
In JavaScript, the task of separating an array of objects into multiple objects involves organizing and restructuring data based on specific criteria or properties. This process is often necessary when dealing with datasets that need to be grouped or segmented for easier analysis or manipulation. There are various methods to achieve this separation
4 min read
How to Remove Multiple Objects from Nested Array of Objects in JavaScript ?
A nested array of objects is an array of arrays that contain multiple objects as their elements. Removing multiple objects from the nested array of objects in JavaSript can be accomplished in different ways as listed below: Table of Content Using filter() with some() methodUsing filter() with includes() methodUsing filter( ) with findIndex() method
5 min read
How to Remove Null Objects from Nested Array of objects in JavaScript ?
Removing null objects from the nested array of objects can be done by iterating over the array, filtering out the null objects at each level, and then applying a recursive approach to remove all the null objects. This makes sure that all the levels of the nested structure are checked and the null objects are removed. There are various approaches to
4 min read
How to Update an Array of Objects with Another Array of Objects using Lodash?
Updating an array of objects with another array of objects involves merging or updating objects in the first array with corresponding objects from the second array. Below are the approaches to update an array of objects with another array of objects in the Lodash library: Table of Content Using _.merge function from lodashUsing _.mergeWith function
2 min read
How to Filter Objects by Data Attribute Value in jQuery ?
In this article, we will learn how to filter objects by their data attribute value in jQuery. There are two approaches that can be taken to achieve this: Approach 1: Using the this property and filter(), data() and css() methods in the jQuery library. There are four division elements that are defined in the following example, each having some data
3 min read
What is the difference between Host objects and Native objects ?
In this article, we will learn about what are Host objects and Native objects, and their differences. JavaScript objects are broadly classified into 2 categories - native javascript objects and host javascript objects. Native objects: Native javascript objects are standard javascript objects which are provided by javascript itself. They are also kn
2 min read
Sort array of objects by string property value in JavaScript
In this article, we will see how to sort an array of objects by string property value in JavaScript. The array of objects can be sorted by using a user-defined function. Here we have some common approaches to sorting an array of objects by string property value Using sort() with a custom compare functionsorting by a user-defined functionUsing sort(
2 min read