js 的数组有一个 fill 方法,可以给数组填充数据,这个方法有三个参数,从左到右依次为 要填充的数据,填充的起始位置(默认为0),停止填充的位置(默认是数组的长度),这个方法不改变原数组,如下:
let arr =[0,0,0,0,0] let res = arr.fill(1,1,2) console.log(res)// [0, 1, 0, 0, 0] let oneParam = arr.fill(1) console.log(oneParam)// [1,1,1,1,1] console.log(arr)// [0,0,0,0,0]