Lodash is a module in Node.js that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.
The Lodash.slice() function is used to take slice of the array from starting index to end index here end index is exclusive and start index is inclusive.
_.slice(array, startIndex, endIndex)
- array: It is the array from which slice is to be taken.
- startIndex: It is the starting index from which slicing of the array starts.
- endIndex: It is the end Index to which slicing is done. Please note endIndex is exclusive.
Return Value: It returns the slice of the array and the return type is array.
Note: Please install lodash module by npm install lodash before using the below given code.
Example 1: Slicing array and the given index size is in range of the array size.
Example 2: Slicing array and the given end index is not in range of the size of the array.
Slicing the empty array
Examples 4: When start and end index are not given.