Javascript object variable name as number

Below I am displayed as "i", not the number that I am repeating. How to fix it? Thank!

for (i = 0; i < 10000; i++) {
     var postParams = {
        i : 'avalueofsorts'
     };
}
+3
source share
2 answers
for (var i = 0, l = 10000; i < l; ++i) {
     var postParams = {};
     postParams[i] = 'avalueofsorts'
}

In a Cybernate comment, you can create an object in advance and just fill it, otherwise you will create it every time. You probably want this:

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

To expand the “do you want comment on an array” checkbox:

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

In javascript, arrays are just objects with several additional methods (push, pop, etc.) and the length property.

0
source

All Articles