开发者

Javascript trouble reversing loop

This loop works 100% fine:

 for(m = 1; m < splitData.length; m++)

This one however throws errors! (Relating to loop body)

for(m = splitData.length; m > 1; m--)

The entire chunk of code is:

// Success
if (splitData[0] == "1") {
    // DbID, username, msg, date

 for(m = splitData.length; m > 1; m--){
        var splitMsg = splitData[m].split("¬");
        $('#<%=discussionBoard.ClientID %>').prepend('<div class="messageWrapper">
             <div class="messageHead">' + splitMsg[1] + '</div>
             <div class="messageTxt">' + 开发者_StackOverflow社区splitMsg[2] + 
             '<div class="messageDetails">' + splitMsg[3] + 
             '</div></div></div>');
 }

The first index in splitData is just a 1 or 0 indicating if Ajax returned good data. I then want to loop through the rest of the input.


You're off-by-one. Remember that the indexes in Javascript are 0-based, which means that they go from 0 to length - 1. Try this instead:

for (m = splitData.length - 1; m >= 1; m--)


Have you tried

for(m = splitData.length - 1; m > 0; m--)

instead of:

for(m = splitData.length; m > 1; m--)


// count from 1 to splitData.length - 1

for(m = 1; m < splitData.length; m++)

// count from splitData.length to 2

for(m = splitData.length; m > 1; m--)

you want

for (m =splitData.length = 1; m > 0; m--)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜