Last updated: Mar 1, 2024
Reading timeยท3 min
The article shows:
id
in an array of objects.To get the max id in an array of objects:
Array.map()
method to get an array of IDs.Math.max()
method.Math.max()
method returns the largest of the given numbers.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
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.
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.
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.
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()
.
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.
You can also use the Array.reduce()
method to get the max id
in an array of
objects.
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
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.
This is a three-step process:
0
.Array.forEach()
method to iterate over the array.id
greater than the id
stored in the
variable, reassign the variable.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 }
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.
You can learn more about the related topics by checking out the following tutorials: