Lodash _.initial() Function
Last Updated :
03 Nov, 2023
Lodash _.initial() function is used to get all elements of the array in the range 0 to n-2. Thereby getting all elements except the (n-1)th element.
Syntax:
Lodash.initial(arrayObject);
Parameters:
- arrayObject: There is only one parameter of type array.
Return Value:
It returns an array object.
Example 1: In this example, When an array of objects is given we are getting all the elements of that array but not the last one because of the use of the lodash _.initial() function.
javascript
const _ = require( 'lodash' );
let array1 = [
{ 'a' : 1, 'b' : 2 },
{ 'a' : 1 },
{ 'b' : 1 }
]
let initialArray = _.initial(array1);
console.log( 'original Array: ' , array1)
console.log( 'initial Array: ' , initialArray)
|
Output:
Example 2: In this example, we are getting all the elements of that array but not the last one because of the use of the lodash _.initial() function.
javascript
const _ = require( 'lodash' );
let array1 = [1, 2, 3, 4]
let initialArray = _.initial(array1);
console.log( 'original Array: ' , array1)
console.log( 'initial Array: ' , initialArray)
|
Output:
Example 3: In this example, we are getting empty array beacuse the original array has only one element.
javascript
const _ = require( 'lodash' );
let array1 = [1]
let initialArray = _.initial(array1);
console.log( 'original Array: ' , array1)
console.log( 'initial Array: ' , initialArray)
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...