我有一个像这样的数组
arr=["rose","AA","lotus","BB","Jasmine","CC",tulip,"DD"];
$.each(arr, function(topics,i){
document.write(topics);
document.write(i);
})
我希望像印刷品一样印刷 玫瑰-AA 莲花BB 茉莉花-CC tuplic-DD
如何在使用.each时将索引增加1?我尝试使用主题++,但它从未奏效。
答案 0 :(得分:1)
尝试一个简单的for循环。
arr=["rose","AA","lotus","BB","Jasmine","CC","tulip","DD"];
for (var i = 0; i < arr.length; i+=2) {
document.write(arr[i] + "-" + arr[i+1] + ' ');
}
答案 1 :(得分:0)
使用简单的for循环进行迭代。(我假设arr的长度总是为偶数)
for(var i=0; i< arr.length; i = i+2){
document.write(arr[i]+"-"+arr[i+1] + " ");
}
答案 2 :(得分:0)
var arr = ["rose", "AA", "lotus", "BB", "Jasmine", "CC", "tulip", "DD"];
for (var i = 0; i < arr.length; i += 2) {
document.write(arr[i] + "-" + arr[i + 1] + ' ');
}
答案 3 :(得分:0)
你可以跳过奇数指数:
$.each(arr, function(topics, i){
if ((topics % 2) == 0) {
document.write(topics);
document.write(i);
}
});