当前位置:网站首页>js如何快速创建一个长度为 n 的数组
js如何快速创建一个长度为 n 的数组
2022-07-06 18:05:00 【闲鱼_JavaScript】
用途
- 快速创建一个循环N次的数组
- 模拟假数据的时候,快捷创建长度为n的假数据
- …
方法一
- 创建一个 长度为10000,值为空 的数组
const a = new Array(10000).fill(''); // (10000) ['', '','', '', '' ....................., '']
- 快速创建指定的内容呢?此处以索引为例子
const a = new Array(10000).fill('').map((val, i) => i); // (10000) [0, 1,2, 3, 4 ....................., 9999]
方法二
- 创建一个 长度为10000,值为空 的数组
const c = Array.from(new Array(10000), () => ''); // (10000) ['', '','', '', '' ....................., '']
- 创建一个 长度为10000,值为索引 的数组
// (...cont) => cont 这个 cont 是把这个函数的所有形参所有形参所有形参都给融进来了(合并成一个数组 为 cont );
// (...cont) => cont 在这里在这里在这里也可以写成 (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]
总结
- 方式一:创建数组,填充空字符串,然后map循环返回新处理后的数组。
- 方式二:通过Array的form静态方法传递两个参数,第一个为数组,第二个参数为处理各项值的回调函数
边栏推荐
- taro3.*中使用 dva 入门级别的哦
- 736. LISP syntax parsing: DFS simulation questions
- Dark horse notes - create immutable sets and streams
- Failed to successfully launch or connect to a child MSBuild. exe process. Verify that the MSBuild. exe
- 系统休眠文件可以删除吗 系统休眠文件怎么删除
- LeetCode:1175. Prime permutation
- Typical problems of subnet division and super network construction
- 数据手册中的词汇
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
- Yunna - work order management system and process, work order management specification
猜你喜欢
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
Analysis of mutex principle in golang
移植DAC芯片MCP4725驱动到NUC980
修改px4飞控的系统时间
云呐|工单管理软件,工单管理软件APP
Gazebo的安装&与ROS的连接
AI 从代码中自动生成注释文档
Transplant DAC chip mcp4725 to nuc980
The difference between Tansig and logsig. Why does BP like to use Tansig
Wood extraction in Halcon
随机推荐
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
curl 命令
Install Firefox browser on raspberry pie /arm device
HMM notes
Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP
Dark horse notes - create immutable sets and streams
hdu 4661 Message Passing(木DP&组合数学)
如何管理分布式团队?
Google released a security update to fix 0 days that have been used in chrome
JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]
Transplant DAC chip mcp4725 to nuc980
Realize incremental data synchronization between MySQL and ES
[signal and system]
AcWing 1148. 秘密的牛奶运输 题解(最小生成树)
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
DS-5/RVDS4.0变量初始化错误
uva 1401 dp+Trie
go-zero微服务实战系列(九、极致优化秒杀性能)
The difference between Tansig and logsig. Why does BP like to use Tansig
Neon Optimization: an instruction optimization case of matrix transpose