Javascript创建数组的方式

不指定具体内容,只是开具体大小空间的数组,如100

1. 使用Array构造函数

        const arr1 = new Array(100);

2. 使用Array.of

        const arr2 = Array.of(...Array(100));

3. 使用Array.from

        const arr3 = Array.from({length:100});

4. 使用fill方法

        const arr4 = new Array(100).fill(undefined);

指定数组内容为下标

1. for循环

        const arr1 = []'

        for(let i = 0; i < 100; i++){

                arr1[i] = i;}

2. Array.from  

const arr2 = Array.from({length:100}, (_,index)=>index))

const arr = Array.from({ length: 30 }, (_, index) => ({ id: index, content: `data-----${index}` }))
<--设多一个id为 v-for-->

3. fill + map

const arr3 = Array(100).fill(0).map((_, index)=>index);

4. 若数组内容要是下标,就可以使用keys()

const arr4 = [...Array(100).keys()];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值