Lodash _.fill() Method
Last Updated :
27 Oct, 2023
Lodash _.fill() method is used to fill a set of values into the array in a given range.
Syntax:
_.fill(array, value, startIndex, endIndex);
Parameters:
- Array: It is the original array that is to be filled with certain values.
- Value: Value to be filled in the array.
- startIndex: It is the index from where the value is to be filled.
- endIndex: It is the index to which values are to be filled in the array.
Note:
- All indexes less than endIndex are included except endIndex.
- Changes are done in the original array.
Return Value:
It returns the array.
Example 1: In this example, we are replacing the first four elements of the given array by 10 by the use of the lodash _.fill() method.
Javascript
let _ = require( "lodash" );
let array = [2, 2, 3, 4, 5, 6]
console.log( "Before : " , array)
_.fill(array, 10, 0, 4)
console.log( "After : " , array)
|
Output:
Example 2: In this example, we are replacing the first ten elements of the given array by 10 by the use of the lodash _.fill() method. and endIndex is greater than the size of array.
Javascript
let lodash = require( "lodash" );
let array = [{ "aa" : 1 },
{ "bb" : 1 }, "a" , "b" ]
console.log( "Before : " , array)
lodash.fill(array, 10, 0, 10)
console.log( "After : " , array)
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...