Friday, September 5, 2025
HomeLanguagesJavascriptLodash _.maxBy() Method

Lodash _.maxBy() Method

Lodash _.maxBy() method is used to compute the maximum value from the original array by iterating over each element in the array using the Iteratee function. It is almost the same as the _.max() function.

Syntax:

_.maxBy( array, [iteratee = _.identity] );

Parameters:

  • array: It is the array that the method iterates over to get the maximum element.
  • iteratee: It is the function that is invoked for every element in the array.

Return Value:

This method returns the maximum element.

Example 1: In this example, we are printing the maximum value of ‘n’ by the use of the _.maxBy() method.

Javascript




// Requiring the lodash library 
const _ = require("lodash");
 
// Original array
let arr = [{ 'n': 4 }, { 'n': 2 }, { 'n': 6 }];
 
// Use of _.maxBy() method
let max_obj = _.maxBy(arr, function (o) { return o.n; });
 
// Printing the output 
console.log(max_obj);


Output:

{ 'n': 6 }

Example 2: In this example, we are printing the maximum value of ‘n’ of an array of objects by the use of the _.maxBy() method.

Javascript




// Requiring the lodash library 
const _ = require("lodash");
 
// Original array
let arr = [{ 'n': 10 }, { 'n': 5 },
{ 'n': 3 }, { 'n': 12 }];
 
// Use of _.maxBy() method
let max_obj = _.maxBy(arr, 'n');
 
// Printing the output 
console.log(max_obj);


Output:

{ 'n': 12 }
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6638 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11866 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7027 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS