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].

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more