Home > Web Front-end > JS Tutorial > How to fill an array with fixed elements in js

How to fill an array with fixed elements in js

醉折花枝作酒筹
Release: 2021-08-11 14:37:50
Original
2115 people have browsed it

In the previous article, we learned about whether elements can pass the test of the specified function. Please see "Can the elements pass the test of the specified function when using js arrays". This time we will learn how to fill an array with fixed values. You can refer to it if you need it.

Before we knew how to convert array elements into strings, how to test whether the elements can pass the specified function, and what the length of the array is. Today we will introduce how to fill the array with fixed elements. What is the method.

First let’s look at a small example.

<script>
var arr = new Array(7); 
arr[0] = "one";
arr[1] = "two";
arr[2] = "three";
console.log(arr.fill("four",3,6));
</script>
Copy after login

The result of this small example is

How to fill an array with fixed elements in js

We can see that the previous results are very normal, among which the third and fourth Fifth, these three elements are all "four", but the last one is empty. Let's take a look at the code again. The fill method we use, our parameter naming selection is "3-6", so why is the last one not filled in?

Let’s take a closer look at this method.

fill() method fills all elements in an array from the starting index to the ending index with a fixed value. Excludes terminating index.

Let’s take a look at the syntax of this method.

arr.fill(用来填充数组元素的值,起始索引,终止索引)
Copy after login

The fill method accepts three parameters, the last two are optional. Their default values ​​are 0 and the value of this object's length attribute. If the start index is negative, the start index is automatically calculated as "array_length_start_index", where array_length is the value of this object's length attribute. If the end index is negative, the end index is automatically calculated as "array length end index".

The fill method is deliberately designed as a general method, which does not require it to be an array object. This method is a mutable method that changes the this object on which it is called and returns it, rather than returning a copy. When an object is passed to the fill method, the array will be filled with a reference to the object.

That’s all. If you need it, you can read: javascript advanced tutorial

The above is the detailed content of How to fill an array with fixed elements in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template