DEV Community

Ajay Marathe
Ajay Marathe

Posted on

Learn Lodash _.drop - Creates a slice of array with n elements dropped from the beginning.

// first Example
  const drop = (arr, n) => {
    for(let i = 0; i < n; i++) {
      arr.shift(arr[i])
    }
    return arr;
  }

  console.log('drop', drop([1, 2, 3], 1))

// second example 
  const drop = (arr, n) => {
    return arr.slice(n)
  }

  console.log('drop', drop([1, 2, 3], 1))
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Function Signature: function drop(arr, n = 1) : This function takes two arguments:
  • arr: The input array from which elements will be dropped.
  • n: The number of elements to drop from the beginning of the array. It defaults to 1 if not provided.
  • Slice Method: The slice method is used to return a shallow copy of a portion of an array into a new array. The method takes two arguments:
  • The start index (n in this case).
  • The end index (not provided here, so it slices to the end of the array).

Example:

  • drop([1, 2, 3], 1) starts the slice at index 1, so it returns [2, 3].

Top comments (0)