In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "JS creates or fills arbitrary length array tips", the explanation content in the article is simple and clear, easy to learn and understand, please follow the idea of Xiaobian slowly in-depth, together to study and learn "JS creates or fills arbitrary length array tips"!
directory
direct filling method
for loop push() method
Array constructor method
Add the fill() method after the Array constructor
Fill an array with undefined
Mapping with Array.from()
Fill an array with values
Create arrays using unique (non-shared) objects
Create arrays from ascending integer series
Create with an arbitrary range of integers
Another way to create ascending integer arrays is to use keys()
direct filling method
In the most primitive way, manually populate an array of the desired length.
const arr = [0,0,0];for loop push() method
Similar 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 构造函数法 var len = 3;var arr = new Array(len);在 Array 构造函数后面加上 fill() 方法var len = 3;var arr = new Array(len).fill(0); 如果你用对象作为参数去 fill() 一个数组,所有元素都会引用同一个实例(也就是这个对象没有被克隆多份,Array.from() 则没有这个问题): var len = 3;var obj = {};var arr = new Array(len).fill(obj); 所以操作这个数组时应该比用构造函数创建的更快。不过创建数组的速度比较慢,因为引擎可能需要随着数组的增长多次重新分配连续的内存。 使用 undefined 填充数组 Array.from({length: 3}) // [ undefined, undefined, undefined ] 下面这种方式仅适用于可迭代的值,并且与 Array.from()具有类似的效果: [...new Array(3)] // [ undefined, undefined, undefined ]使用 Array.from() 进行映射 如果提供映射函数作为其第二个参数,则可以使用 Array.from() 进行映射。 用值填充数组Array.from({length: 3}, () =>0) // [ 0, 0, 0 ] Create Array.from({length: 3}, () => ({})) using unique (non-shared) objects // [ {}, {}, {} ] Create Array.from({length: 3}, (x, i) => i) in ascending integer series // [ 0, 1, 2 ] Create with any range of integers var start = 2, end = 5;Array.from({ length: end - start }, (x, i) => i + start) // [ 2, 3, 4 ] Another way to create an array of ascending integers is to use keys()[...] new Array(3).keys()] // [ 0, 1, 2 ] Thank you for reading, the above is "JS create or fill arbitrary length array tips what" content, after learning this article, I believe that we create or fill arbitrary length array tips what this problem has a deeper understanding, the specific use of the need for everyone to practice verification. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.