// 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].
The above is the detailed content of Learn Lodash _.drop - Creates a slice of array with n elements dropped from the beginning.. For more information, please follow other related articles on the PHP Chinese website!
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn