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
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Introduction to Tree Data Structure Tree data structure is a hierarchical structure that is used to represent and organize data in the form of parent child relationship. The following are some real world situations which are naturally a tree.Folder structure in an operating system.Tag structure in an HTML (root tag the as html tag) or
15+ min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
Web Development Technologies Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.To better understand the foundation of web devel
7 min read