开发者

Javascript object variable name as number

开发者_Go百科

Below, i shows up as "i", not the number I am iterating through. How do I correct this? Thanks!

for (i = 0; i < 10000; i++) {
     var postParams = {
        i : 'avalueofsorts'
     };
}


for (var i = 0, l = 10000; i < l; ++i) {
     var postParams = {};
     postParams[i] = 'avalueofsorts'
}

Per Cybernate's comment, you can create the object beforehand and just populate it otherwise you create it each time. You probably want this:

for (var i = 0, l = 10000, postParams = {}; i < l; ++i) {
     postParams[i] = 'avalueofsorts'
}


To expand on the 'you want an array comment':

for (var i = 0, postParams = []; i < 10000; i++) {
     postParams.push('avalueofsorts');
}

In javascript arrays are just objects with a few extra methods (push, pop etc...) and a length property.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜