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
Refer to the official documentation here for oper...
It is mainly the configuration jump of the if jud...
Routing configuration commands under Linux 1. Add...
Forms are a major external form for implementing ...
The function to be implemented today is the follo...
<template> <div id="root"> ...
1. Download and install Download the community ed...
Preface Tomcat is a widely used Java web containe...
Today I will introduce a very simple trick to con...
Because li is a block-level element and occupies ...
I have encountered a problem. When testing the ed...
I call this kind of bug a typical "Hamlet&qu...
Yesterday, I helped someone install a system and ...
Table of contents 1. Introduction 2. Output Infor...
Table of contents 1. Instructions 2. Modifiers 3....