我做了一个 for 循环来生成点。现在我想以特定的方式给它们上色。就像前 200 个是黑色的,接下来的 326 个是蓝色的。我已经做到了,但现在我有很多行代码,我想知道是否有更干净的方法来做到这一点。例如,为颜色创建一个数组,然后为值创建一个数组?
let circleColor = 'Lavender ';
const secondColor = 'burlyWood ';
const numberFirstColor = 493;
const numberSecondColor = numberFirstColor+578;
for (let i = 0; i < circleCount; i++) {
if (i > numberFirstColor) circleColor = secondColor;
if ( i > numberSecondColor) circleColor= thirdColor;
最佳答案
您可以保留一个对象数组,然后对其进行迭代:
for(const { color, count } of [{ color: "Lavender", count: 200 }, { color: "burlyWood", count: 326 } /*..*/]) {
for(let i = 0; i < count; i++) {
circleColor = color;
//...
}
}
关于javascript - 如何组合两个数组的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56360248/