Js traversal elements are empty arrays (because I only need to use the index)

problem description

need to use the path of the picture, but there are many pictures, but the format is / static/pic/bg0 1 2 3 4 5

the environmental background of the problems and what methods you have tried

I tried to traverse with the following code, but failed

related codes

const arr = new Array(6)
const imgsPath = arr.map((item, index) => `/static/pic/bg${index}`)

what result do you expect? What is the error message actually seen?

expect / static/pic/bg0 to 5 in imgsPath, a total of 6 path strings, but the arr.map method is not executed. So in the end, it can only be written as const arr = [0,1,2,3,4,5] to solve this problem.
what if I have 100 such paths to deal with in the future?

Apr.02,2021

    (new Array(100)).fill(0).map((_, index) => `/static/pic/bg${index}`)

const imgsPath = new Array(6).fill('').map((item, index) => `/static/pic/bg${index}`);
console.log(imgsPath)

Why do you have to write a for loop in map,.


wouldn't it be nice to just use the for loop directly?

const imgLen = 6
const imgsPath = []
for(let i=0;i<imgLen;iPP){
    imgsPath.push(`/static/pic/bg${i}`)
}
Menu