Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter, and Return Value
3.1.
Syntax:
3.2.
Parameters:
3.3.
Return Value:
4.
Examples
4.1.
Basic Usage:
4.2.
JavaScript
4.3.
Specifying Start and End:
4.4.
JavaScript
4.5.
Working with Object Arrays:
4.6.
JavaScript
4.7.
Using Function as Value:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
Can _.fill() be used on arrays of mixed types?
5.2.
Does _.fill() modify the original array?
5.3.
How does _.fill() differ from native JavaScript methods?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.fill() Method

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Lodash, a prominent JavaScript utility library, offers a multitude of functions that streamline common programming tasks. Among these, the _.fill() method stands out for its utility in manipulating arrays. This method provides a straightforward way to modify elements in an array, replacing them with a given value. It's particularly useful in scenarios where you need to initialize or reset array elements efficiently. 

Lodash _.fill() Method

Let's delve into the specifics of this method to understand its functionality and applications better.

Why This Function is Used

The _.fill() method is primarily used for modifying all elements in a portion of an array with a new value. This function is essential in situations where:

  • Initializing Arrays: Quickly set all elements of an array to a specific value.
     
  • Data Manipulation: Overwrite a section of an array, which is helpful in data preparation and transformation tasks.
     
  • Testing and Mock Data: Easily create arrays with dummy data for testing purposes.
     
  • UI Development: Uniformly update elements in data structures that reflect UI components.

This method enhances code readability and efficiency, eliminating the need for explicit loops to achieve the same outcome.

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

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

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

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

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 DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts

Live masterclass