使用JavaScript 快速创建一个1到100的数组

代码示例:

let arr = []
// push方法
for(let i = 1,len=100;i<=100;i++){arr.push(i)}
//  or
//循环赋值
for(let i = 1,len=100;i<=100;i++){arr[i-1]=i}
// Array.from()方法 后面会有一点点的优化
Array.from({length:101}, (v,k) => k)// or Array.from(Array(101), (v,k) =>k);
Array.from(Array(101).keys())
arr.splice(0,1)

// 使用递归实现
// 当前值
// 最大长度
let arr = []
function setArray(current,maxLength,arr){
  if(arr.length<maxLength){
    arr[current-1] = current
    setArray(++current,maxLength,arr)
  }else{
      arr.filter(i=>i)// 去除假值 empty
  }
}
setArray(1,100,arr)
// 暂且不知道有没有性能问题

// 在网上看到一些方法,也蛮好玩的,但是不知道来源

// 1
let arr = Array(100).toString().split(',').map((item,index)=>index)// 0-> 99

// 2
let i = 0;
let timer = setInterval(function(){
  arr[i] = i++;
  if(i>=100){
    clearInterval(timer);
    console.log(arr);
  }
},1); // 0-> 99

// 3
Object.keys(Array.apply(null, {length:100})).map(item=>+item);// 0-> 99

// 4
[...Array(100).keys()]// 0-> 99
[...Array.from({length:100}).keys()]// 0-> 99

// mdn 看见的 序列生成器(指定范围)
const range = (start, stop, step) => Array.from({ length: (stop - start) / step + 1}, (_, i) =>  + (i * step));

range(1,100,1)// 1-> 100

//优化我上面写的
Array.from({length:100},(_, i)=>1+(i)) // 1-> 100

我觉得看起来最舒服的是: Array.from({length:100},(_, i)=>1+(i))


前提知识:

Array.from()

Array.from(arrayLike[, mapFn[, thisArg]])

  • arrayLike

    想要转换成数组的伪数组对象或可迭代对象。

  • mapFn 可选

    如果指定了该参数,新数组中的每个元素会执行该回调函数。

  • thisArg 可选

    可选参数,执行回调函数 mapFnthis 对象。

    返回值

    一个新的数组实例。

用法:

  • String 生成 数组
  • Set 生成 数组
  • Map 生成 数组
  • 类数组 生成 数组

示例:从Map生成数组

const map = new Map([[1, 2], [2, 4], [4, 8]]);
Array.from(map);
// [[1, 2], [2, 4], [4, 8]]

const mapper = new Map([['1', 'a'], ['2', 'b']]);
Array.from(mapper.values());
// ['a', 'b'];

Array.from(mapper.keys());
// ['1', '2'];

其他:

Array.from({length: 100}) or  Array.from(Array(100))

// 都会生成
(100) [undefined,..., undefined]
数组实例方法keys()

keys()是ES6中新增的对键名的遍历,返回一个遍历器对象

apply(thisArg, [argsArray])

apply(thisArg, [argsArray])

参数

  • thisArg

    必选的。在 func 函数运行时使用的 this 值。请注意,this可能不是该方法看到的实际值:如果这个函数处于非严格模式下,则指定为 nullundefined 时会自动替换为指向全局对象,原始值会被包装。

  • argsArray

    可选的。一个数组或者类数组对象,其中的数组元素将作为单独的参数传给 func 函数。如果该参数的值为 nullundefined,则表示不需要传入任何参数。从ECMAScript 5 开始可以使用类数组对象。

返回值

调用有指定this值和参数的函数的结果。

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值