当前位置:网站首页>JS how to quickly create an array with length n
JS how to quickly create an array with length n
2022-07-07 01:41:00 【Idle fish_ JavaScript】
purpose
- Quickly create a loop N Array of times
- When simulating false data , Quick creation length is n False data of
- …
Method 1
- Create a The length is 10000, Value is empty Array of
const a = new Array(10000).fill(''); // (10000) ['', '','', '', '' ....................., '']
- Quickly create the specified content ? Take the index as an example
const a = new Array(10000).fill('').map((val, i) => i); // (10000) [0, 1,2, 3, 4 ....................., 9999]
Method 2
- Create a The length is 10000, Value is empty Array of
const c = Array.from(new Array(10000), () => ''); // (10000) ['', '','', '', '' ....................., '']
- Create a The length is 10000, The value is index Array of
// (...cont) => cont This cont Is to integrate all parameters of this function, all parameters, all parameters ( Merge into an array by cont );
// (...cont) => cont It can also be written here, here, here (val, i) => [val, i];
const c = Array.from(new Array(10000), (...cont) => cont[1]); // (10000) [0, 1,2, 3, 4 ....................., 9999]
const c = Array.from(new Array(10000), (val, i) => i); // (10000) [0, 1,2, 3, 4 ....................., 9999]
summary
- Mode one : Create array , Fill empty string , then map Loop back the newly processed array .
- Mode two : adopt Array Of form Static methods pass two parameters , The first is an array , The second parameter is the callback function for processing various values
边栏推荐
- JS Es5 can also create constants?
- 剑指 Offer II 035. 最小时间差-快速排序加数据转换
- Yunna - work order management system and process, work order management specification
- AcWing 1148. Secret milk transportation problem solution (minimum spanning tree)
- Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period
- Google发布安全更新,修复Chrome中已被利用的0 day
- AcWing 1141. LAN problem solving (kruskalkruskal finding the minimum spanning tree)
- Typical problems of subnet division and super network construction
- Instructions for using the domain analysis tool bloodhound
- js如何快速创建一个长度为 n 的数组
猜你喜欢
Baidu flying general BMN timing action positioning framework | data preparation and training guide (Part 2)
黑马笔记---异常处理
【C语言进阶篇】指针的8道笔试题
百度飞将BMN时序动作定位框架 | 数据准备与训练指南 (上)
AcWing 1148. Secret milk transportation problem solution (minimum spanning tree)
云呐-工单管理制度及流程,工单管理规范
鼠标右键 自定义
454 Baidu Mianjing 1
Right mouse button customization
子网划分、构造超网 典型题
随机推荐
LeetCode:1175. Prime permutation
爬虫实战(六):爬笔趣阁小说
Taro applet enables wxml code compression
C语言实例_4
Google发布安全更新,修复Chrome中已被利用的0 day
How to prevent overfitting in cross validation
THREE. AxesHelper is not a constructor
百度飞将BMN时序动作定位框架 | 数据准备与训练指南 (上)
C language instance_ three
C语言实例_2
JVM 内存模型
AcWing 344. 观光之旅题解(floyd求无向图的最小环问题)
剑指 Offer II 035. 最小时间差-快速排序加数据转换
mysqlbackup 还原特定的表
AcWing 1140. 最短网络 (最小生成树)
数据手册中的词汇
安全保护能力是什么意思?等保不同级别保护能力分别是怎样?
The difference between Tansig and logsig. Why does BP like to use Tansig
免费白嫖的图床对比
修改px4飞控的系统时间