introduction
In JavaScript, there are many ways to create and fill arrays, such as usingfor
Recycle, use()
Methods, useArray()
Constructors, etc. In this article, we will explore some ways to quickly and efficiently create one-dimensional arrays.
Use the () method
()
is a JavaScript built-in method for creating new arrays from similar arrays or iterable objects. We can use it to create a filled 1D array. Here is an example:
const createArray = (length, start = 0, end = length) => ({ length: end - start }, (_, i) => i + start); (createArray(10)); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (createArray(5, 2, 7)); // [2, 3, 4, 5, 6]
In this example, we define a name calledcreateArray
The function has three parameters:length
Indicates the length of the array,start
andend
Indicates the start and end values that need to be filled into the array. If not specifiedstart
andend
, then the default fill is0
arrivelength - 1
incremental number of . Then we use()
Method to create a size ofend - start
array, used in callback functioni + start
to fill in the incremental number of each element. Finally, we return the filled one-dimensional array.
Use Array() to construct the function
Another way to create a one-dimensional array is to useArray()
Constructor. This method accepts an integer parameter that represents the length of the array. Here is an example:
const createArray = (length, start = 0, end = length) => Array(end - start) .fill() .map((_, i) => i + start); (createArray(10)); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (createArray(5, 2, 7)); // [2, 3, 4, 5, 6]
In this example, we define a name calledcreateArray
The function has three parameters:length
Indicates the length of the array,start
andend
Indicates the start and end values that need to be filled into the array. If not specifiedstart
andend
, then the default fill is0
arrivelength - 1
incremental number of . Then we useArray()
Constructor to create a size ofend - start
array and use.fill()
Method fills each element asundefined
. Finally, we use.map()
Method to replace each element in the array with incremental numbers and return a filled one-dimensional array.
It should be noted that this approach may degrade performance slightly, as it requires creating a full intermediate array that is populated and then used.map()
The method is replaced by an incremental number. If performance is a problem, then you can use other methods to fill the array, e.g.for
cycle.
Using for loops
usefor
Looping is also a way to create one-dimensional arrays. Here is an example:
const createArray = (length, start = 0, end = length) => { const arr = []; for (let i = start; i < end; i++) { (i); } return arr; }; (createArray(10)); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (createArray(5, 2, 7)); // [2, 3, 4, 5, 6]
In this example, we define a name calledcreateArray
The function has three parameters:length
Indicates the length of the array,start
andend
Indicates the start and end values that need to be filled into the array. If not specifiedstart
andend
, then the default fill is0
arrivelength - 1
incremental number of . Then we create an empty arrayarr
and usefor
The loop willstart
arriveend
The numbers between them are added to the array in turn. Finally, we return the filled one-dimensional array.
It should be noted that this approach may slightly reduce the readability of the code and in some cases may cause some minor performance issues. If you want more readable and concise code, then consider using()
orArray()
method.
In general, use()
orArray()
Methods are the best way to create a one-dimensional array. They are simple and efficient, and are better than use in terms of readabilityfor
cycle. Depending on the specific situation, you can choose one of the methods to fill your array.
in conclusion
In JavaScript, there are many ways to create and fill arrays, which we describe in this article()
method,Array()
Constructor andfor
These three common ways to cycle. In actual development, you can choose the most suitable method to fill the array according to the actual situation to achieve higher performance and readability. Hopefully this article will help you gain a deeper understanding of how to create one-dimensional arrays quickly and efficiently.
The above is a detailed explanation of how to quickly and efficiently create JavaScript one-dimensional array methods. For more information about JavaScript one-dimensional array creation, please pay attention to my other related articles!