Thursday, September 25, 2025
HomeLanguagesJavascriptLodash _.initial() Function

Lodash _.initial() Function

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.
The Lodash.initial() function is used to get the all elements of the array in 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.

Note: Install lodash module by npm install lodash before using the below-given code.

Example 1: When an array of objects is given.




// 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 = lodash.initial(array1);
  
// Original Array
console.log("original Array: ", array1)
  
// Printing the initialArray
console.log("initial Array: ", initialArray)


Output:

Example 2: 




// 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 = lodash.initial(array1);
  
// Original Array
console.log("original Array: ", array1)
  
// Printing the initialArray
console.log("initial Array: ", initialArray)


Output: 

Example 3: When an array of size one is given it returns the empty array.




// 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 = lodash.initial(array1);
  
// Original Array
console.log("original Array: ", array1)
  
// Printing the initialArray
console.log("initial Array: ", initialArray)


Output: 

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
Previous article
Next article
RELATED ARTICLES

Most Popular

Dominic
32319 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6682 POSTS0 COMMENTS
Nicole Veronica
11854 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11910 POSTS0 COMMENTS
Shaida Kate Naidoo
6795 POSTS0 COMMENTS
Ted Musemwa
7071 POSTS0 COMMENTS
Thapelo Manthata
6753 POSTS0 COMMENTS
Umr Jansen
6761 POSTS0 COMMENTS