Syntax, Parameter, and Return Value
Syntax:
_.fill(array, value, [start=0], [end=array.length])
Parameters:
-
array (Array): The array to be filled.
-
value (*): The value to fill the array with.
-
start (optional, number): The start position, default is 0.
-
end (optional, number): The end position, default is the array's length.
Return Value:
The modified array with the elements from start to end replaced with value.
Examples
Basic Usage:
Filling an array with a static value.
JavaScript
let numbers = [1, 2, 3, 4, 5];
_.fill(numbers, 'a');
console.log(numbers);
You can also try this code with Online Javascript Compiler
Run Code
Output:
['a', 'a', 'a', 'a', 'a']
Specifying Start and End:
Filling an array from a specific start to end index.
JavaScript
let numbers = [1, 2, 3, 4, 5];
_.fill(numbers, 'a', 2, 4);
console.log(numbers);
You can also try this code with Online Javascript Compiler
Run Code
Output:
[1, 2, 'a', 'a', 5]
Working with Object Arrays:
Filling an array of objects.
JavaScript
let objects = [{ x: 1 }, { x: 2 }, { x: 3 }];
_.fill(objects, { y: 2 });
console.log(objects);
You can also try this code with Online Javascript Compiler
Run Code
Output:
[{ y: 2 }, { y: 2 }, { y: 2 }]
Using Function as Value:
Using a function to generate values.
JavaScript
let numbers = [1, 2, 3, 4, 5];
_.fill(numbers, () => Math.random());
console.log(numbers);
You can also try this code with Online Javascript Compiler
Run Code
Output:
Random values in each element
Frequently Asked Questions
Can _.fill() be used on arrays of mixed types?
Yes, _.fill() can operate on arrays containing different types of elements, uniformly replacing them with the specified value.
Does _.fill() modify the original array?
Yes, it modifies the original array in place, and it's advisable to use it cautiously to avoid unintended side effects.
How does _.fill() differ from native JavaScript methods?
While similar to JavaScript's Array.prototype.fill(), _.fill() offers consistent behavior across different JavaScript environments, ensuring compatibility.
Conclusion
The Lodash _.fill() method is a versatile tool for array manipulation. Its ability to efficiently replace elements in an array with a specified value makes it an invaluable asset in a developer's toolkit. Whether it's initializing arrays, preparing data, or creating test scenarios, _.fill() simplifies these tasks, enhancing code readability and maintainability. Its straightforward syntax and flexible usage adapt well to various programming needs, making it a preferred choice for array operations.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts