listOfObjects = listOfObjects.concat(Array(10000000).fill({l: 50, b: 50, color: "#000000"}).map((s) => {
s.x = Math.floor(Math.random() * (mapSquareSize));
s.y = Math.floor(Math.random() * (mapSquareSize));
return s;
}));
The listOfObjects array should have objects with the properties x,y, l, b and color. I have used the Array fill() function to set the l, b and color variables. I want the x and y co-ordinates to be unique for each object in ListOfObjects. However, the above code just gives the same random x and y co-ordinates to each Object.
I was using this for loop for this purpose, but a friend told me that a solution like this would be way more efficient.
// for (var i = 1 ; i <= 10000000 ; i++)
// {
// var rock = new Object();
// rock.x = Math.floor(Math.random() * (mapSquareSize)) + 0 ;
// rock.y = Math.floor(Math.random() * (mapSquareSize)) + 0 ;
// rock.l = 50 ;
// rock.b = 50 ;
// rock.color = "#000000";
// listOfObjects.push(rock);
// }
Array.fill()like that, every element in the array will be a reference to the exact same object. The.fill()approach is not going to be more efficient than your working simpleforloop anyway.forloop looks fine to me; anything that operates on each element of an array is essentially doing the same amount of work.