0
var data = [
{
color: "black"
width: 0
},
{
color: "black"
width: 50
},
{
color: "black"
width: 100
},
]
案例之間
該函數接受的輸入是一個數字。這個數字需要與它所在的數據點進行比較。
條件:
我們知道它每次會增加多少。在這種情況下,它是50.
我們不知道這裏有多少個對象。在這種情況下有3
它總是從0開始
這是可以完成它的靜態功能(輸入= 55):
loopThis(input){
if(input < 50){
for(var i in data){
data[i].color = "black";
}
data[0].color = "red";
}else if(input > 50 && input < 100){
for(var i in data){
data[i].color = "black";
}
data[1].color = "red";
}else{
for(var i in data){
data[i].color = "black";
}
data[2].color = "red";
}
}
這將導致第二數據對象(data [1])變成紅色,其餘的將變成黑色。
如何把上面的條件和我如何使這個代碼動態?
如果我們在數組中有4個對象,這將不起作用。而且我們無法知道每次寫入多少條件。
不要使用'parseInt'進行舍入。 – 2017-05-05 11:20:51
您可能需要管理邊界條件,例如50,100等。在上面的登錄輸入50中,將標記數據[1] .color =「red」。 而不是parseInt使用Math.round()。 –
'Math.floor(input/50)' – yezzz