使用 Array.from 快速生成数组

一般我们生成一个有规律的数组会使用循环插入的方法,比如使用时间选择插件时,我们可能需要将小时数存放在数组中:

let hours = [];

for (let i = 0; i < 24; i++) {
    
    
    hours.push(i + '时');
}

如果使用 Array.from 我们可以简写为:

let hours = Array.from({
    
     length: 24 }, (value, index) => index + '时');

猜你喜欢

转载自blog.csdn.net/fd2025/article/details/125730681