Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

lodash
communitycreator

What is the _.fill() method in Lodash?

Shubham Singh Kshatriya

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The _.fill() method in Lodash is used to fill an array with a given value from the start of the array, and up to but excluding the end of the array.

Syntax

_.fill(array, value, start, end)

Parameters

This method accepts the following parameters:

  • array: This is the array to be filled.
  • value: This is the value to be filled in the array.
  • start: This is the start from where the value is to be filled in the array. If not specified, it will take 0 as default.
  • end: This is the end till where the value is to be filled in the array. If not specified, it will take the length of the array as the default.

    Return value

    This method returns an updated array.

    Example

    Let’s look at an example of the _.fill() method in the code snippet below.

    Console
    Implementation of the _.fill() method

    Explanation

    In the HTML tab, we see the following.

    • Line 5: We import the lodash script.

    In the JavaScript tab, we see the following.

    • Line 2: We create an array and initialize it with a few values.
    • Line 5: We create a variable value and initialize it with *.
    • Line 8: We create a variable start and initialize it with 2.
    • Line 11: We create a variable end and initialize it with 4.
    • Line 14: We use the _.fill() method to fill the array.
    • Line 17: We print the output to the console.

    Output

    We see an array that is filled with the given value, from the given start, and up to but excluding the end.

    RELATED TAGS

    lodash
    communitycreator

    CONTRIBUTOR

    Shubham Singh Kshatriya

    Grokking Modern System Design Interview for Engineers & Managers

    Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

    Keep Exploring