A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/lodash-_-initial-function/ below:

Lodash _.initial() Function - GeeksforGeeks

Lodash _.initial() Function

Last Updated : 02 Sep, 2024

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: Return Value:

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

javascript
// 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.

javascript
// 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: 



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4