Thursday, November 13, 2025
HomeLanguagesJavascriptCollect.js max() Method

Collect.js max() Method

The max() method is used to return the maximum value of a given key. It takes a collection and returns the maximum values present in that collection for a given key.

Syntax:

collect(array).max(key)

Parameters: The collect() method takes one argument that is converted into the collection and then the max() method is applied to it. The max() method can hold the key of the object.

Return Value: This method returns the maximum value of a given key. 

Module Installation: Install collect.js module using the following command from the root directory of your project:

npm install collect.js

The below example illustrates the max() method in collect.js:

Example 1: Filename: index.js

Javascript




// Requiring the module
const collect = require('collect.js');
  
// Array with sample values
let arr = [10, 15, -20, -25, 40, 50, -70];
  
// Creating collection object
const collection = collect(arr);
  
// Function call
const max_val = collection.max();
  
// Printing the max value
console.log(max_val);


Run the index.js file using the following command:

node index.js

Output:

50

Example 2: Filename: index.js

Javascript




// Requiring the module
const collect = require('collect.js');
  
// Array with sample values
let arr = [
  {
      name: 'Rahul',
      dob: '25-10-96',
      section: 'A',
      score: 98,
  },
  {
      name: 'Aditya',
      dob: '25-10-96',
      section: 'B',
      score: 96,
  },
  {
      name: 'Abhishek',
      dob: '16-08-94',
      section: 'A',
      score: 80
  }
];
  
// Creating collection object
const collection = collect(arr);
  
// Function call
const max_val = collection.max('score');
  
// Printing the max value
console.log(max_val);


Run the index.js file using the following command:

node index.js

Output:

98
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
32399 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6765 POSTS0 COMMENTS
Nicole Veronica
11916 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11984 POSTS0 COMMENTS
Shaida Kate Naidoo
6889 POSTS0 COMMENTS
Ted Musemwa
7141 POSTS0 COMMENTS
Thapelo Manthata
6837 POSTS0 COMMENTS
Umr Jansen
6839 POSTS0 COMMENTS