How to Get Index of the Max Value in Array of Objects ? Last Updated : 04 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report When dealing with arrays of objects in JavaScript, it's common to need the index of the object with the maximum value based on a certain property. Below are the approaches to get the index of the max value in an array of objects: Table of Content Using a LoopUsing Array.reduce() MethodUsing a LoopThis approach iterates through the array of objects using a simple for loop. It keeps track of the index of an object with the maximum value based on the specified property. Example: The below code uses a for loop to iterate over the array of objects and get the index of the max value. JavaScript function indexOfMax(arr, prop) { let max = -Infinity; let index = -1; for (let i = 0; i < arr.length; i++) { if (arr[i][prop] > max) { max = arr[i][prop]; index = i; } } return index; } const employees = [ { name: 'John', salary: 50000 }, { name: 'Alice', salary: 70000 }, { name: 'Bob', salary: 60000 } ]; const maxSalaryIndex = indexOfMax(employees, 'salary'); console.log("Index of employee with highest salary:", maxSalaryIndex); OutputIndex of employee with highest salary: 1 Using Array.reduce() MethodArray.reduce() is a powerful method in JavaScript used to reduce the elements of an array to a single value. It executes a provided function for each value of the array and accumulates a single result. Syntax:Array.reduce(callbackFunction, initialValue)Example: The below code explains the use of the reduce() method to get the index of the max value in an array of objects. JavaScript function indexOfMax(arr, prop) { return arr.reduce( (maxIndex, current, currentIndex, array) => { return current[prop] > array[maxIndex][prop] ? currentIndex : maxIndex; }, 0); } const people = [ { name: 'Alice', age: 30 }, { name: 'Bob', age: 40 }, { name: 'John', age: 35 } ]; const maxAgeIndex = indexOfMax(people, 'age'); console.log("Index of person with maximum age:", maxAgeIndex); OutputIndex of person with maximum age: 1 Comment More infoAdvertise with us Next Article How to Get Index of the Max Value in Array of Objects ? R rohitdalvi Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions 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 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 Max/Min value of an attribute in an array of objects in JavaScript 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:using JavaScript apply() and Array map()using JavaScript Array reduc 3 min read How to get the last item of JavaScript object ? In this article, we will learn how to get the last item of a Javascript object. Given a JavaScript object and the task is to get the last element of the JavaScript object. This can be done by the following methods: Using Object.keys() methodUsing for loopmethodApproach 1: Use Object.keys() method to 2 min read Array.LastIndexOf Method in C# | Set - 1 Array.LastIndexOf method is used to find the last matching element in a one-dimensional array. It starts the search from the last element of an array. It returns the index of the element that contains the specified value. There are 6 methods in the overload list of this method as follows: LastIndexO 8 min read Like