0

我有一个这样的数组

arr=["rose","AA","lotus","BB","Jasmine","CC",tulip,"DD"];

$.each(arr, function(topics,i){
             document.write(topics);
             document.write(i);           

            })

我想要像
玫瑰-AA lotus-BB Jasmine-CC tuplic-DD这样的东西打印

如何在使用 .each 时将索引增加 1?我尝试使用主题++,但它从未奏效。

4

4 回答 4

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] + ' ');
}
于 2012-10-26T19:30:46.040 回答
0

使用简单的 for 循环进行迭代。(我假设 arr 的长度总是偶数)

for(var i=0; i< arr.length; i = i+2){
         document.write(arr[i]+"-"+arr[i+1] + " ");

}
于 2012-10-26T19:31:59.687 回答
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] + ' ');
    }
于 2012-10-26T19:32:38.270 回答
0

您可以跳过奇数索引:

$.each(arr, function(topics, i){
  if ((topics % 2) == 0) {
    document.write(topics);
    document.write(i);           
  }
});

演示:http: //jsfiddle.net/Guffa/VT9tT/

于 2012-10-26T19:34:25.373 回答