To remove n
number of elements from beginning or end of an array in Javascript, you may use these methods provided by Lodash β
_.drop(array, n)
_.dropRight(array, n)
_.dropRightWhile(array, predicate)
-
_.dropWhile(array, predicate)
These 4 functions are used to drop first or last few numbers of elements.
How it is decided to remove, what number of elements?
There are two strategies β
1. Specifying the count of elements to drop, like 5 from beginning or 2 from end. _.drop
and _.dropRight
use this.
2. A predicate function which runs a logic to determine up to which element, array should drop. So, the array removes the elements as long as this function is returning true. Check the below explanation β
Suppose predicate function is returning true for values less than 10.
const predicateFunc = (val, index, arr) => (val < 10)
Our array is -
const arr = [2, 4, 6, 10, 14, 8, 5, 18]
Running predicateFunc over each value of array -
[2, π 2 < 10 = True π Drop β
4, π 4 < 10 = True π Drop β
6, π 6 < 10 = True π Drop β
10, π 10 < 10 = False π Retain β
14, π NA π β
8, π NA π β
5, π NA π β
18] π NA π β
So, final array = [10, 14, 8, 5, 18]
You can see that 8, 5 are smaller than 10 but still in final array. Because as soon as the first false
is achieved, predicateFunc won't run on further values.
_.dropRightWhile
and _.dropWhile
uses predicate function to decide the elements to drop.
_.drop(array, n)
These are the properties β
- It removes
n
number of elements from the beginning of the array. - The default value of
n
is 1. - Put n=0 to prevent dropping any value.
Code Example
const arr = [2, 4, 6, 10, 14, 8, 5, 18] console.log(_.drop(arr, 3)) // Output π [10, 14, 8, 5, 18] console.log(_.drop(arr)) // Output π [4, 6, 10, 14, 8, 5, 18] // Because default value of n=1 console.log(_.drop(arr, 0)) // Output π [2, 4, 6, 10, 14, 8, 5, 18] console.log(_.drop(arr, 100)) // Output π []
_.dropRight(array, n)
All the properties of this function are similar to _.drop
except it removes elements from the end.
Code Example
const arr = [2, 4, 6, 10, 14, 8, 5, 18] console.log(_.dropRight(arr, 3)) // Output π [2, 4, 6, 10, 14] console.log(_.dropRight(arr)) // Output π [2, 4, 6, 10, 14, 8, 5] // Because default value of n=1 console.log(_.dropRight(arr, 0)) // Output π [2, 4, 6, 10, 14, 8, 5, 18] console.log(_.dropRight(arr, 100)) // Output π []
_.dropWhile(array, predicate)
This function is like _.drop
but instead of using number of elements to drop, it uses predicate function.
We already discussed about predicate functions in previous section. Letβs see some code examples.
Code Example
const arr = [2, 4, 6, 10, 14, 8, 5, 18] const predicateFunc1 = (val, index, arr) => (val < 10) console.log(_.dropWhile(arr, predicateFunc1)) // Output π [10, 14, 8, 5, 18] /*************************************************/ const arr2 = [ {a: 1, b: 2, c: 5}, {a: 2, b: 3, c: 6}, {a: 3, b: 4, c: 7}, {a: 4, b: 5, c: 8} ] const predicateFunc2 = (val, index, arr) => (val.a + val.b != val.c) // predicateFunc2 will return true if a + b != c. // For val {a: 1, b: 2, c: 5}, a + b = 1 + 2 = 3 // c = 5. So, a + b != c. console.log(_.dropWhile(arr2, predicateFunc2)) // Output π /* [ {a: 3, b: 4, c: 7}, {a: 4, b: 5, c: 8} ] */
_.dropRightWhile(array, predicate)
This is exactly like _.dropWhile
except it drops from the end or right side. Not from the beginning.
Code Example
const arr = [2, 4, 6, 10, 14, 8, 5, 18] const predicateFunc1 = (val, index, arr) => (val < 10) console.log(_.dropRightWhile(arr, predicateFunc1)) // Output π [2, 4, 6, 10, 14, 8, 5, 18] /*************************************************/ const arr2 = [ {a: 1, b: 2, c: 5}, {a: 2, b: 3, c: 6}, {a: 3, b: 4, c: 7}, {a: 4, b: 5, c: 8} ] const predicateFunc2 = (val, index, arr) => (val.a + val.b != val.c) console.log(_.dropRightWhile(arr2, predicateFunc2)) // Output π /* [ {a: 1, b: 2, c: 5}, {a: 2, b: 3, c: 6}, {a: 3, b: 4, c: 7} ] */