Thursday, January 9, 2025
Google search engine
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

Recent Comments