The _.slice()
method in Lodash is used to create a slice of an array from the start, and up to but excluding the end.
_.slice(array, start, end)
This method accepts the following parameters:
array
: This is the array to be sliced.start
: This is the start from where the array will be sliced. If not specified, it will take 0 as default.end
: This is the end until where the array will be sliced. If not specified, it will take the length of the array as the default.This method returns a slice of an array.
Let’s look at an example of the _.slice()
method in the code snippet below.
In the HTML tab, we see the following.
lodash
script.In the JavaScript tab, we see the following.
start
and initialize it with 2
.end
and initialize it with 4
._.slice()
method to slice the array.In the output, we see a slice of the array.