Saturday, November 23, 2024
Google search engine
HomeLanguagesJavascriptLodash _.takeWhile() Method

Lodash _.takeWhile() Method

The _.taketWhile the method is used to create a slice of an array in which elements are taken from the beginning. Also, these elements are taken until predicate returns falsely.

Syntax:

_.takeWhile(array, [predicate=_.identity])

Parameters: This method accepts two parameters as mentioned above and described below:

  • array: This parameter holds the array to query.
  • [predicate=_.identity]: This parameter holds the function invoked per iteration.

Return Value: This method is used to return the slice of array.

Example 1: Here, const _ = require(‘lodash’) is used to import the lodash library in the file.

javascript




// Requiring the lodash library 
const _ = require("lodash"); 
      
// Original array 
var users = [
  { 'user': 'jupiter''active': false },
  { 'user': 'mercury',    'active': false },
  { 'user': 'saturn', 'active': true }
];
      
  
// Use of _.takeWhile() 
// method
let gfg = _.takeWhile(users, function(o) { return !o.active; });
      
// Printing the output 
console.log(gfg);


Output:

[{ user: 'jupiter', active: false },
 {user: 'mercury', active: false}]

Example 2:

javascript




// Requiring the lodash library 
const _ = require("lodash"); 
      
// Original array 
var users = [
  { 'user': 'jupiter''active': false },
  { 'user': 'mercury',    'active': false },
  { 'user': 'saturn', 'active': true }
];
      
  
// Use of _.takeWhile() 
// method
// The `_.matches` iteratee shorthand.
   
let gfg = _.takeWhile(users, { 'user': 'jupiter', 'active': false });
      
// Printing the output 
console.log(gfg);


Output:

[{ user: 'jupiter', active: false }]

Example 3:

javascript




// Requiring the lodash library 
const _ = require("lodash"); 
      
// Original array 
var users = [
  { 'user': 'jupiter''active': false },
  { 'user': 'mercury',    'active': false },
  { 'user': 'saturn', 'active': true }
];
      
  
// Use of _.takeWhile() 
// method
// The `_.matchesProperty` iteratee shorthand.
  
let gfg = _.takeWhile(users, ['active', false]);
      
// Printing the output 
console.log(gfg);


Output:

[{ user: 'jupiter', active: false },
 {user: 'mercury', active: false}]

Example 4:

javascript




// Requiring the lodash library 
const _ = require("lodash"); 
      
// Original array 
var users = [
  { 'user': 'jupiter''active': false },
  { 'user': 'mercury',    'active': false },
  { 'user': 'saturn', 'active': true }
];
      
  
// Use of _.takeWhile() 
// method
// The `_.property` iteratee shorthand.
  
let gfg = _.takeWhile(users, 'active');
      
// Printing the output 
console.log(gfg);


Output:

[]

Note: This code will not work in normal JavaScript because it requires the library lodash to be installed.

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

Recent Comments