js array fill() filling method

js array fill() filling method

Preface:

We know a lot about how to initialize arrays, but after initializing the array, each element in the array defaults to an empty placeholder. How can we add default elements to these empty places in the array? ES6 provides fill() method to achieve this operation. This article summarizes the detailed use of the array fill() method.

1. fill() syntax

fill() method fills all elements in an array from the start index to the end index with a fixed value. The ending index is not included. Returns the original array modified without creating a new array.

Use the syntax: array.fill( value [,start [,end]]), where:

  • value is used to fill the value of array elements, required.
  • start is an optional starting index, the default value is 0.
  • end optional ending index, defaults to this.length .

2. Use of fill()

   // When a single parameter is passed, the method fills the entire array with the value of that parameter var arr1 = new Array(5)
    console.log(arr1.fill(1)); //[1,1,1,1,1]
    var arr2 = [1, 2, 3, 4]
    console.log(arr2.fill(0)); //[0,0,0,0]

    // When two parameters are passed in, the first parameter is the element to be filled, and the second is the starting position of the filled element var arr3 = [0, 1, 2, 3, 4, 5, 6]
    console.log(arr3.fill(1, 3)); //[0,1,2,1,1,1,1]

    // When three parameters are passed in, the first parameter is the element to be filled, the second and third parameters refer to the starting and ending positions of the filled element respectively, and the ending position element is not modified var arr4 = [0, 1, 2, 3, 4, 5]
    console.log(arr4.fill(1, 3, 5)); //[0,1,2,1,1,5]
    
    //If the starting position or ending position provided is a negative number, the length of the array will be added to them to calculate the final position. For example, a starting position of -1 is equivalent to array.length-1
    var arr5 = [0, 1, 2, 3, 4, 5]
    console.log(arr5.fill(1, -3));//[0,1,2,1,1,1]
    var arr6 = [0, 1, 2, 3, 4, 5]
    console.log(arr6.fill(1, 3, -2));//[0,1,2,1,4,5]

3. Summary

The above is the full content of this article. I hope it can bring some help and progress to the readers. If it is convenient, please follow me. Xiaobai’s Growth Path will continue to update some common problems and technical points in work.

This is the end of this article about the js array fill() filling method. For more related js array fill() filling content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Complete list of javascript array methods
  • JS one-dimensional array converted to three-dimensional array method
  • Detailed Example of JavaScript Array Methods
  • Detailed explanation of JS array methods
  • Detailed explanation of common methods of JavaScript arrays
  • Let's learn about javascript array methods

<<:  Delete the image operation of none in docker images

>>:  mysql8.0.23 msi installation super detailed tutorial

Recommend

Node.js returns different data according to different request paths.

Table of contents 1. Learn to return different da...

Detailed explanation of the functions of each port of Tomcat

From the tomcat configuration file, we can see th...

HTML Tutorial: Definition List

<br />Original text: http://andymao.com/andy...

Five ways to implement inheritance in js

Borrowing Constructors The basic idea of ​​this t...

Native JavaScript to achieve slide effects

When we create a page, especially a homepage, we ...

Web design tips on form input boxes

This article lists some tips and codes about form...

Function overloading in TypeScript

Table of contents 1. Function signature 2. Functi...

24 Practical JavaScript Development Tips

Table of contents 1. Initialize the array 2. Arra...

js implements custom drop-down box

This article example shares the specific code of ...

5 super useful open source Docker tools highly recommended

Introduction The Docker community has created man...

Detailed examples of variable and function promotion in JavaScript

js execution Lexical analysis phase: includes thr...

Practice using Golang to play with Docker API

Table of contents Installing the SDK Managing loc...

Introduction to the use of MySQL pt-slave-restart tool

Table of contents When setting up a MySQL master-...

MySQL decimal unsigned update negative numbers converted to 0

Today, when verifying the concurrency problem of ...

Example of JSON output in HTML format (test interface)

To display the JSON data in a beautiful indented ...