Nodejs lodash.slice() function

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.

Syntax: 

_.slice(array, startIndex, endIndex)

Parameters:

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

javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Requiring the lodash library
let lodash= require("lodash");
 
// Original array
let array1 = [[1,12],[12,8],7,8]
//using lodash.slice()
let newArray=lodash.slice(array1,1,3);
//printing original Array
console.log("original Array1: ",array1)
//printing the newArray
console.log("new Array: ",newArray)

chevron_right


Output: 

Example 2: 

Slicing array and the given end index is not in range of the size of the array
 

javascript



filter_none

edit
close

play_arrow

link
brightness_4
code

//requiring the lodash library
let lodash= require("lodash");
//original array
let array1 = [[1,12],[12,8],7,8,3,4]
//using lodash.slice()
let newArray=lodash.slice(array1,1,10);
//printing original Array
console.log("original Array1: ",array1)
//printing the newArray
console.log("new Array: ",newArray)

chevron_right


Output: 

Example 3: 
Slicing the empty array

javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

//requiring the lodash library
let lodash= require("lodash");
//original array
let array1 = []
//using lodash.slice()
let newArray=lodash.slice(array1,1,2);
//printing original Array
console.log("original Array1: ",array1)
//printing the newArray
console.log("new Array: ",newArray)

chevron_right


Output: 

Examples 4:

When start and end index are not given. 

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

//requiring the lodash library
let lodash= require("lodash");
//original array
let array1 = [1,2,4,3,1,5]
//using lodash.slice()
let newArray=lodash.slice(array1);
//printing original Array
console.log("original Array1: ",array1)
//printing the newArray
console.log("new Array: ",newArray)

chevron_right


Output :

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.