Friday, September 5, 2025
HomeLanguagesJavascriptCollect.js take() Method

Collect.js take() Method

The take() method is used to return a new collection with the specified number of items. It returns the elements from the end of the collection if the argument passed as a parameter is negative.

Syntax:

collect.take()

Parameters: The collect() method takes one argument that is converted into the collection and then the take() method is applied to it.

Return Value: This method returns a new collection with the specified number of items.

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 take() method in collect.js:

Example 1: Filename: index.js

Javascript




// Requiring the module
const collect = require('collect.js'); 
    
// Sample array
let arr = [2, 4, 5, 6, 7, 8, 9]; 
    
// Creating collection
const collection = collect(arr); 
  
// Function call
const result = collection.take(5)
  
// Printing the result object
let newObject = result.all();
console.log(newObject);


Run the index.js file using the following command:

node index.js

Output:

[2, 4, 5, 6, 7]

Example 2: Filename: index.js

Javascript




// Requiring the module
const collect = require('collect.js'); 
    
// Sample array
let arr = ['a', 'b', 'c', -3, 4, -6, 7]; 
    
// Creating collection
const collection = collect(arr); 
  
// Function call
const result = collection.take(-3)
  
// Printing the result object
let newObject = result.all();
console.log(newObject);


Run the index.js file using the following command:

node index.js

Output:

[4, -6,  7]
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
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11861 POSTS0 COMMENTS
Shaida Kate Naidoo
6750 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS