1

I know this is probably really difficult, because I want something like Object1, Object2, etc. This is what I think might work, but probably won't.

for(var x = 1; x<=10; x++){
    var Object + x = new Object();
};

What should I do instead. Thanks in advance!

3
  • 1
    Use an array and on each iteration, create the object and push to that array. Commented Feb 16, 2014 at 11:55
  • 1
    This is called a variable variable name. Javascript does not support this functionality in the way you're trying to do it. It does support arrays with string indices. So you can do something more like objects["Object + x"] = new Object(); Commented Feb 16, 2014 at 11:55
  • If working in the global scope : window['Object' + x] = .... Commented Feb 16, 2014 at 12:02

1 Answer 1

2
var objects = {};

for (var x = 0; x < 100; x++) {
  objects[x] = {name: "value"};  //value can be a string like "value" or a var like value 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.