Last Updated : 02 Sep, 2024
Lodash _.slice() function is used to take a slice of the array from the starting index to the end index here end index is exclusive and the start index is inclusive.
Syntax:_.slice(array, startIndex, endIndex);Parameters:
Example 1: In this example, we are slicing an array and the given index size is in the range of the array size.
javascript
// Requiring the lodash library
let lodash = require("lodash");
// Original array
let array1 = [[1, 12], [12, 8], 7, 8]
// Using lodash.slice() method
let newArray = lodash.slice(array1, 1, 3);
// Printing original Array
console.log("original Array1: ", array1)
// Printing the newArray
console.log("new Array: ", newArray)
Output:
Example 2: In this example, we are slicing an array and the given end index is not in range of the size of the array.
// Requiring the lodash library
let lodash = require("lodash");
// Original array
let array1 = [[1, 12], [12, 8], 7, 8, 3, 4]
// Using lodash.slice() method
let newArray = lodash.slice(array1, 1, 10);
// Printing original Array
console.log("original Array1: ", array1)
// Printing the newArray
console.log("new Array: ", newArray)
Output:
Example 3: In this example, we are slicing the empty array.
// Requiring the lodash library
let lodash = require("lodash");
// Original array
let array1 = []
// Using lodash.slice() method
let newArray = lodash.slice(array1, 1, 2);
// Printing original Array
console.log("original Array1: ", array1)
// Printing the newArray
console.log("new Array: ", newArray)
Output:
Examples 4: In this example, we are slicing an array where start and end index are not given.
JavaScript
// Requiring the lodash library
let lodash = require("lodash");
// Original array
let array1 = [1, 2, 4, 3, 1, 5]
// Using lodash.slice() method
let newArray = lodash.slice(array1);
// Printing original Array
console.log("original Array1: ", array1)
// Printing the newArray
console.log("new Array: ", newArray)
Output:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4