// Initializing with one object
let posi_val=[{top: '3px', left: '2px'}];
// Adding another object in the loop
for (let i = 1; i < n; i++) {
posi_val.push({
top: `${posi_val[i - 1].top.slice(0, -2) * 2}px`,
left: `${posi_val[i - 1].left.slice(0, -2) * 2}px`
});
}
Hey everyone! The code snippet above is part of a CSS class creation process to generate multiple red dots at different positions. I'm looking for guidance on how to expand the array of objects like the ones shown above using a for loop, where each subsequent value for 'top' and 'left' properties will be twice the previous value (e.g., 3, 6, 12 px and 2, 4, 8 px).
I attempted using JSON.parse to reconstruct the entire string, but it got quite confusing. Are there simpler methods available to construct this array efficiently? You can check out a demo on CodePen here.