Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

lodash
communitycreator

How to use the dropWhile() method in Lodash

Shubham Singh Kshatriya

Overview

The _.dropWhile() method in Lodash creates a slice of an array by dropping all the elements from the beginning until the specified predicate returns a false value.

Syntax

_.dropWhile(array, predicate)
Syntax of the _.dropWhile() method

Parameters

This method accepts the following parameters:

  • array: This is the array to be sliced.
  • predicate: This is the function that is checked against the values of the array in every iteration.

Return value

This method returns a slice of an array.

Example

Let’s look at an example of the _.dropWhile() method in the code snippet below:

Console
Implementation of the _.dropWhile() method

Explanation

In the HTML tab:

  • Line 5: We import the lodash library.

In the JavaScript tab:

  • Lines 2–7: We create an array of objects.
  • Line 10: We use the _.dropWhile() method to create a slice.
  • Line 13: We print the output to the console.

Output

The _.dropWhile() method drops all the elements from the people array until the predicate encounters a person with an inactive status. This is the third element in our example.

In the output, therefore, we see an array containing two objects. The objects preceding { name: 'Rachel', designation: 'Content writer', status: 'inactive'} are dropped.

    RELATED TAGS

    lodash
    communitycreator

    CONTRIBUTOR

    Shubham Singh Kshatriya
    RELATED COURSES

    View all Courses

    Keep Exploring