Método Lodash _.takeWhile()

El método _.taketWhile se utiliza para crear una porción de una array en la que los elementos se toman desde el principio. Además, estos elementos se toman hasta que el predicado devuelve falso.

Sintaxis:

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

Parámetros: este método acepta dos parámetros, como se mencionó anteriormente y se describe a continuación:

  • array: este parámetro contiene la array para consultar.
  • [predicado=_.identidad]: este parámetro contiene la función invocada por iteración.

Valor de retorno: este método se utiliza para devolver el segmento de array.

Ejemplo 1: Aquí, const _ = require(‘lodash’) se usa para importar la biblioteca lodash en el archivo.

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);

Producción:

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

Ejemplo 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);

Producción:

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

Ejemplo 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);

Producción:

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

Ejemplo 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);

Producción:

[]

Nota: este código no funcionará en JavaScript normal porque requiere que se instale la biblioteca lodash.

Publicación traducida automáticamente

Artículo escrito por shivanisinghss2110 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *