Open In App

Collect.js takeUntil() Method

Last Updated : 02 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The takeUntil() method is used to return the items in the collection until the given callback returns true. If the given value is not found or the callback never returns true, the takeUntil() method will return all items in the collection.

Syntax:

collect.takeUntil()

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

Return Value: This method returns the items in the collection.

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

Example 1: Filename: index.js

Javascript




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


Run the index.js file using the following command:

node index.js

Output:

[1, 2, 3, 4, 5, 6]

Example 2: 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
    .takeUntil(item => item == 7);
  
// 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]


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads