PrefaceIn JavaScript development, there are often scenarios where you need to create arrays of a specific length. This article summarizes several tips for creating or filling arrays of arbitrary length. Learning these tips can improve your programming efficiency. Direct filling methodTake the most primitive approach and manually fill the array to the required length. const arr = [0,0,0]; for loop push() methodSimilar to the first method, but using a for loop to create an array of a specific length var len = 3; var arr = []; for (let i=0; i < len; i++) { arr.push(0); } Array Constructor Methodvar len = 3; var arr = new Array(len); Add fill() method after Array constructorvar len = 3; var arr = new Array(len).fill(0); If you use an object as a parameter to fill() an array, all elements will refer to the same instance (that is, the object is not cloned multiple times, Array.from() does not have this problem): var len = 3; var obj = {}; var arr = new Array(len).fill(obj); So operating on this array should be faster than creating it using the constructor. However, creating arrays is slower because the engine may need to reallocate contiguous memory multiple times as the array grows. Filling an array with undefinedArray.from({length: 3}) // [ undefined, undefined, undefined ] The following approach only works for iterable values, and has a similar effect to Array.from(): [...new Array(3)] // [ undefined, undefined, undefined ] Mapping with Array.from()You can map using Array.from() if you provide a mapping function as its second argument. Filling an array with valuesArray.from({length: 3}, () => 0) // [ 0, 0, 0 ] Creating an array with unique (non-shared) objectsArray.from({length: 3}, () => ({})) // [ {}, {}, {} ] Create an array with ascending integer sequencesArray.from({length: 3}, (x, i) => i) // [ 0, 1, 2 ] Create with any range of integersvar start = 2, end = 5; Array.from({ length: end - start }, (x, i) => i + start) // [ 2, 3, 4 ] Another way to create an ascending integer array is to use keys()[...new Array(3).keys()] // [ 0, 1, 2 ] SummarizeThis concludes this article on tips for creating or filling arrays of arbitrary length with JS. For more information on creating and filling arrays with JS, please search previous articles on 123WORDPRESS.COM or continue browsing the related articles below. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Example of creating table statements for user Scott in MySQL version of Oracle
>>: Robots.txt detailed introduction
Table of contents 1. Introduction 2. Prepare a pr...
Table of contents Preface 1. Basic knowledge of d...
First time writing. Allow me to introduce myself....
How to add a loading animation every time I scrol...
Preface The project requires a circular menu. I s...
Update: Recently, it was discovered that the serv...
This article uses an example to describe how to c...
Scenario simulation: Some domestic companies need...
Deploy database based on docker sudo docker pull ...
Table of contents 1. We found that this website m...
One environment Install VMware Tools on CentOS 7 ...
A simple calculator written in WeChat applet for ...
There is a new build tool in the Vue ecosystem ca...
Preface What is data type conversion? The default...
Optimize the fastcgi configuration file fcgiext.i...