3

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'
     };
}
1
  • are you sure you want to create a new object every iteration? Commented Jun 3, 2011 at 16:44

2 Answers 2

7
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'
}
Sign up to request clarification or add additional context in comments.

3 Comments

@Meder: I guess the postParams should be declared outside the for loop other wise it gets defined for every iteration.
@Frank ~ Note that you're not using an object here, but an array, really. Just a comment.
Yea, going from Ruby Hashes to Java structs to this I'm trying to figure out semantics still . Thanks :)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.