Thursday, July 4, 2024
HomeLanguagesJavascriptUnderscore.js _.dropWhile() Method

Underscore.js _.dropWhile() Method

The _.dropWhile() method takes an array and a function and hence returns an array in which elements are dropped from the original array for which the function returns true.

Note: values are dropped until the function returns false, after a false value no changes to the original value are made.

Syntax:

_.dropWhile(array, function)

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

  • array: The given array from which dropped array is created.
  • function: The function containing the conditions for elements to be dropped.

Return Value: This method returns a newly created array.

Note: This will not work in normal JavaScript because it requires the underscore.js contrib library to be installed. 

underscore.js contrib library can be installed using:

npm install underscore-contrib

Example 1: In this example, we will create an array with all positive values dropped.

Javascript




// Defining underscore contrib variable
const _ = require('underscore-contrib');
// Array
let array = [11, 21, 43, 34, 12, -1];
// Getting dropped array using dropWhile() method
let d_array = _.dropWhile(array, function (x) {
    return x > 0;
});
console.log("Array : ", array);
console.log("Dropped Array : ", d_array);


Output:

Array :  [ 11, 21, 43, 34, 12, -1 ]
Dropped Array :  [ -1 ]

Example 2: In this example, we will create an array with all negative values dropped.

Javascript




// Defining underscore contrib variable
const _ = require('underscore-contrib');
// Array
let array = [-1, -21, -43, 34, 12, -1];
// Getting dropped array using dropWhile() method
let d_array = _.dropWhile(array, function (x) {
    return x < 0;
});
console.log("Array : ", array);
console.log("Dropped Array : ", d_array);


Output: negative values are dropped until a positive value comes.

Array :  [ -1, -21, -43, 34, 12, -1 ]
Dropped Array :  [ 34, 12, -1 ]

Example 3: In this example, we will create an array with 0 values dropped until the function returns false.

Javascript




// Defining underscore contrib variable
const _ = require('underscore-contrib');
// Array
let array = [0, 0, 0, 0, 0, -1, -21, -43, 34, 12, -1];
// Getting dropped array using dropWhile() method
let d_array = _.dropWhile(array, function (x) {
    return x == 0;
});
console.log("Array : ", array);
console.log("Dropped Array : ", d_array);


Output: 

Array :  [
   0,   0,   0,  0,  0,
  -1, -21, -43, 34, 12,
  -1
]
Dropped Array :  [ -1, -21, -43, 34, 12, -1 ]

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!

Nango Kalahttps://www.kala.co.za
Experienced Support Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Microsoft Excel, Customer Service, Microsoft Word, Technical Support, and Microsoft Office. Strong information technology professional with a Microsoft Certificate Solutions Expert (Privet Cloud) focused in Information Technology from Broadband Collage Of Technology.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments