Get the Max id in an Array of Objects in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 1, 2024
3 min

banner

# Table of Contents

  1. Get the Max id in an Array of Objects in JavaScript
  2. Get the Max id in an Array of Objects using reduce()
  3. Get the Max id in an Array of Objects using forEach()

The article shows:

  1. How to get the Max/Min id in an array of objects.
  2. How to get the Max/Min values in an object.

# Get the Max id in an Array of Objects in JavaScript

To get the max id in an array of objects:

  1. Use the Array.map() method to get an array of IDs.
  2. Pass the array of IDs as an argument to the Math.max() method.
  3. The Math.max() method returns the largest of the given numbers.
index.js
const arr = [{id: 1}, {id: 7}, {id: 3}, {id: 14}]; const ids = arr.map(object => { return object.id; }); console.log(ids); // ๐Ÿ‘‰๏ธ [1, 7, 3, 14] const max = Math.max(...ids); console.log(max); // ๐Ÿ‘‰๏ธ 14 const min = Math.min(...ids); console.log(min); // ๐Ÿ‘‰๏ธ 1

get max id in array of objects

The code for this article is available on GitHub
If you also need the min object ID, call the Math.min() method with the same argument.

If you need to find the matching object, use the Array.find() method.

index.js
const arr = [{id: 1}, {id: 7}, {id: 3}, {id: 14}]; const ids = arr.map(object => { return object.id; }); const max = Math.max(...ids); console.log(max); // ๐Ÿ‘‰๏ธ 14 // โœ… Find the object with the max ID const obj = arr.find(element => element.id === max); console.log(obj); // ๐Ÿ‘‰๏ธ { id: 14 }

We used the Array.map() method to get an array containing the IDs of all objects.

index.js
const arr = [{id: 1}, {id: 7}, {id: 3}, {id: 14}]; const ids = arr.map(object => { return object.id; }); console.log(ids); // ๐Ÿ‘‰๏ธ [1, 7, 3, 14]

On each iteration, we access the id property on the current object and return the result.

We used the spread syntax (...) in the call to the Math.max() method.

The Math.max() method expects comma-separated numbers as parameters, so we can't directly pass it an array.

index.js
const max = Math.max(1, 7, 3, 14) console.log(max) // ๐Ÿ‘‰๏ธ 14

We used the spread syntax (...) to unpack the values of the array in the call to Math.max().

index.js
const arr = [{id: 1}, {id: 7}, {id: 3}, {id: 14}]; const ids = arr.map(object => { return object.id; }); console.log(ids); // ๐Ÿ‘‰๏ธ [1, 7, 3, 14] const max = Math.max(...ids); console.log(max); // ๐Ÿ‘‰๏ธ 14

You can imagine that the values from the array get passed as multiple, comma-separated arguments to the Math.max() method.

# Get the Max id in an Array of Objects using reduce()

You can also use the Array.reduce() method to get the max id in an array of objects.

index.js
const arr = [{id: 1}, {id: 7}, {id: 3}, {id: 14}]; const maxObj = arr.reduce((accumulator, current) => { return accumulator.id > current.id ? accumulator : current; }); console.log(maxObj); // ๐Ÿ‘‰๏ธ { id: 14 } console.log(maxObj.id); // ๐Ÿ‘‰๏ธ 14

get max id in array of objects using reduce

The code for this article is available on GitHub

The function we passed to the Array.reduce() method gets called for each element in the array.

The value we return from the callback function gets passed as the accumulator on the next iteration.

On each iteration, we check if the accumulator's id is greater than the current object's id property.

If the condition is met, the accumulator object is returned, otherwise, the current object is returned.

The reduce() method returns the object with the max id in the array.

# Get the Max id in an Array of Objects using forEach()

This is a three-step process:

  1. Declare a new variable and initialize it to 0.
  2. Use the Array.forEach() method to iterate over the array.
  3. If the current object has an id greater than the id stored in the variable, reassign the variable.
index.js
const arr = [{id: 1}, {id: 7}, {id: 3}, {id: 14}]; let maxId = 0; let maxObj; arr.forEach(element => { if (element.id > maxId) { maxId = element.id; maxObj = element; } }); console.log(maxId); // ๐Ÿ‘‰๏ธ 14 console.log(maxObj); // ๐Ÿ‘‰๏ธ { id: 14 }

get max id in array of objects using foreach

The code for this article is available on GitHub

Notice that we used the let keyword when declaring the maxId and maxObj variables.

Using let is important because variables declared using const cannot be reassigned.

On each iteration, we check if the current object has an id property greater than the value stored in the maxId variable.

If the condition is met, we reassign the two variables.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev