如何不跳过年份数组的元素?
我希望结果是:
<com.like.LikeButton
app:icon_type="heart"
app:icon_size="25dp"
android:id="@+id/heart_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
当前结果:
Banana
0
Mango
1
此处的代码:
Banana
0
Mango
3
答案 0 :(得分:2)
像您一样遍历数组,并在水果与case语句之一匹配时增加第二个计数器。使用计数器从数组中获取年份数字:
var fruits, text, year, i;
fruits = ["Banana", "Orange", "Apple", "Mango"];
year = ["0", "1", "2", "3"];
text = "<ul>";
let counter = 0; // <-- define a second counter
for (i = 0; i < fruits.length; i++) {
switch (fruits[i]) {
case 'Mango':
text += "<li>" + fruits[i] + "</li>";
text += "<li><b>" + year[counter] + "</b></li>";
counter++; // <-- that only get's incremented when a case is matched
break;
case 'Pine apple':
text += "<li>" + fruits[i] + "</li>";
text += "<li><b>" + year[counter] + "</b></li>";
counter++;
break;
case 'Grape':
text += "<li>" + fruits[i] + "</li>";
text += "<li><b>" + year[counter] + "</b></li>";
counter++;
break;
case 'Banana':
text += "<li>" + fruits[i] + "</li>";
text += "<li><b>" + year[counter] + "</b></li>";
counter++;
break;
}
}
text += "</ul>";
document.getElementById("demo").innerHTML = text;
<p id="demo"></p>