0

I am trying to dynamically build an object for every iteration of a for loop using the i as part of the object name. based on the example below I would like 19 objects with names: obj0, obj1, obj2... obj18.

so I have an array with a length:

    console.log(foo.length); // 19

    for (var i = 0; i < foo.length; i++) {
        var bar+i  = {}; 
    };
    console.log(bar1);
    console.log(bar2);
    // ...
    console.log(bar18); 

I can't figure out the correct syntax for "var bar+i = {};", obviously it does not work.

EDIT I really need objects because I am constructing data to be used in D3.js that needs to be an array of many objects

2
  • 3
    Perhaps you should be creating an array. Commented Dec 13, 2013 at 15:03
  • 1
    Is there any reason these need to be global? Or not in an array? Commented Dec 13, 2013 at 15:03

5 Answers 5

2

Unless bar{i} is an array value / object property itself the only way to do this is to bind it to the window or root object.

window[bar+i] = {};

Or to the root object

this[bar+i] = {};

Much better to bind it to an array / object itself though rather than bind senselessly to the root/window.

var array = [];

array[bar+i] = {};
Sign up to request clarification or add additional context in comments.

1 Comment

Binding to an array is just what I needed to do, your third comment did just the trick, with one small change: array["bar"+i] = {};
2

There are some hacks how you can achieve this. However I advice you to use arrays instead of that method you are trying to use:

 console.log(foo.length); // 19

 var variables = [];

 for (var i = 0; i < foo.length; i++) {
    variables[i]  = {}; 
 };
 console.log(variables[0]);
 console.log(variables[1]);
 // ...
 console.log(variables[18]); 

2 Comments

He doesn't have to use arrays. He can use an object instead... but yeah.. the rest is correct.
Creating an array from literal ([]) is faster then new Array().
2

You can't create variables like this.

What you can do is add properties of the global scope, which you can use as variables:

for (var i = 0; i < foo.length; i++) {
  window['bar'+i ] = {}; 
};

or use another object to hold everything:

var container = {};
for (var i = 0; i < foo.length; i++) {
  container['bar'+i] = {}; 
};

Comments

1

You can't dynamically write variable names, but you can do the same on object properties:

console.log(foo.length); // 19

var obj = {};

for (var i = 0; i < foo.length; i++) {
    obj[ 'bar' + i ] = {}; 
};

console.log(obj.bar1);
console.log(obj.bar2);
// ...
console.log(obj.bar18); 

Comments

0

You can use eval:

eval('var bar'+i+' = {};');

But really, you shouldn't be doing this, unless you know for sure you can't do it the other way.

2 Comments

This answer is correct, but it's kind-of like, "Well, you can do it by putting on the One Ring of Power, but please don't because MORDOR."
@Pointy Because MORDOR will get you?

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.