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.
// Requiring the lodash library const _ = require( 'lodash' );
// Original array let array1 = [ { 'a' : 1, 'b' : 2 },
{ 'a' : 1 },
{ 'b' : 1 }
] // Using _.initial() function to get // all elements of the array in // range (0,n-2]) let initialArray = _.initial(array1); // Original Array console.log( 'original Array: ' , array1)
// Printing the initialArray 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.
// Requiring the lodash library const _ = require( 'lodash' );
// Original array let array1 = [1, 2, 3, 4] // Using _.initial() function to get // all elements of the array in // range (0,n-2]) let initialArray = _.initial(array1); // Original Array console.log( 'original Array: ' , array1)
// Printing the initialArray console.log( 'initial Array: ' , initialArray)
|
Output:
Example 3: In this example, we are getting empty array beacuse the original array has only one element.
// Requiring the lodash library const _ = require( 'lodash' );
// Original array let array1 = [1] // Using _.initial() function to get // all elements of the array in // range (0,n-2]) let initialArray = _.initial(array1); // Original Array console.log( 'original Array: ' , array1)
// Printing the initialArray console.log( 'initial Array: ' , initialArray)
|
Output: