A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/lodash-_-dropwhile-method/ below:

Lodash _.dropWhile() Method - GeeksforGeeks

Lodash _.dropWhile() Method

Last Updated : 30 Aug, 2024

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc. The Loadsh.dropWhile() method is used to return the slice of the given array. This takes a predicate function that iterates through each element of the array and if the function returns false, it returns the sliced array excluding elements dropped from the beginning.

Syntax:

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

Parameters:

Return Value: It returns the new array after slicing.

Note: Please install lodash module by using command npm install lodash before using the code given below.

Example 1: 

JavaScript
// Requiring the lodash library
const _ = require("lodash");

// Original array
let array1 = [1, 3, 4, 5, 5, 6]

// Using _.dropWhile() method
let newArray = _.dropWhile(array1, (e) => {
    return e != 5;
});

// Original Array
console.log("original Array: ", array1)

// Printing the newArray
console.log("new Array: ", newArray)

Output: 

Example 2: When array of object is given.

JavaScript
// Requiring the lodash library
const _ = require("lodash");

// Original array
let array1 = [
    { "a": 1, "b": 2 }, 
    { "a": 2, "b": 1 }, 
    { "b": 2 }
]

// Using _.dropWhile() method
let newArray = _.dropWhile(array1, (e) => {
    return e.b == 2;
});

// Original Array
console.log("original Array: ", array1)

// Printing the newArray
console.log("new Array: ", newArray)

Output: 



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4