I want let the random number of array keep refresh,but it didn't work. Can someone give me some advice?
var places=Array(50).fill({}).map(
function(obj){
return {
r: Math.random()*200,
deg: Math.random()*360,
opacity: 0
}
}
);
If you are trying to create array of objects with random values, you can do it:
The normal while loop method:
// declare empty array for later use
const places = []
// iterate 50 times, for each iteration,
// create and add one object with random values into array
let i = 0
while(i < 50) {
places.push({
r: Math.random() * 200,
deg: Math.random() * 360,
opacity: 0
})
i++
}
The Array.from method:
// first argument define the length of array
// second argument define function to execute for every iteration
const places = Array.from({length: 50}, () => {
return {
r: Math.random() * 200,
deg: Math.random() * 360,
opacity: 0
}
})