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(…


This content originally appeared on DEV Community and was authored by Ajay Marathe

// 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))

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


This content originally appeared on DEV Community and was authored by Ajay Marathe


Print Share Comment Cite Upload Translate Updates
APA

Ajay Marathe | Sciencx (2024-08-10T23:06:27+00:00) Learn Lodash _.drop – Creates a slice of array with n elements dropped from the beginning.. Retrieved from https://www.scien.cx/2024/08/10/learn-lodash-_-drop-creates-a-slice-of-array-with-n-elements-dropped-from-the-beginning/

MLA
" » Learn Lodash _.drop – Creates a slice of array with n elements dropped from the beginning.." Ajay Marathe | Sciencx - Saturday August 10, 2024, https://www.scien.cx/2024/08/10/learn-lodash-_-drop-creates-a-slice-of-array-with-n-elements-dropped-from-the-beginning/
HARVARD
Ajay Marathe | Sciencx Saturday August 10, 2024 » Learn Lodash _.drop – Creates a slice of array with n elements dropped from the beginning.., viewed ,<https://www.scien.cx/2024/08/10/learn-lodash-_-drop-creates-a-slice-of-array-with-n-elements-dropped-from-the-beginning/>
VANCOUVER
Ajay Marathe | Sciencx - » Learn Lodash _.drop – Creates a slice of array with n elements dropped from the beginning.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/10/learn-lodash-_-drop-creates-a-slice-of-array-with-n-elements-dropped-from-the-beginning/
CHICAGO
" » Learn Lodash _.drop – Creates a slice of array with n elements dropped from the beginning.." Ajay Marathe | Sciencx - Accessed . https://www.scien.cx/2024/08/10/learn-lodash-_-drop-creates-a-slice-of-array-with-n-elements-dropped-from-the-beginning/
IEEE
" » Learn Lodash _.drop – Creates a slice of array with n elements dropped from the beginning.." Ajay Marathe | Sciencx [Online]. Available: https://www.scien.cx/2024/08/10/learn-lodash-_-drop-creates-a-slice-of-array-with-n-elements-dropped-from-the-beginning/. [Accessed: ]
rf:citation
» Learn Lodash _.drop – Creates a slice of array with n elements dropped from the beginning. | Ajay Marathe | Sciencx | https://www.scien.cx/2024/08/10/learn-lodash-_-drop-creates-a-slice-of-array-with-n-elements-dropped-from-the-beginning/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.