0

i am trying to create new Objects with names out of an array. Without an array i would do:

var object_bruno = new Object();
var object_carlos = new Object();
var object_luci = new Object();

so i will end up with 3 new Objects. But why wont we do that with an loop, which makes it more easy to adde some more Objects later. So i ttried:

//      an array full of object names
var obj_arr = [ "object_bruno", "object_carlos", "object_luci"];

//      Method one:
for (x in obj_arr) {
    alert(obj_arr[x]);      //  right names shown
    var obj_arr[x] = new Object();  //syntax error, dosent work??
};

//      Method two:
obj_arr.forEach(function(func_name) {
    alert(func_name);       //  right names
    var func_name = new Object();   //  no objects are created ???
});

basicly i would prefer to use Method two. i like it because i can fill them late the same way? hopefuly? Any ideas what wents wrong?

2
  • Please add your code in fiddle .. That is very helpful for people to understand your problem Commented Oct 28, 2015 at 7:35
  • jsfiddle.net/krL4fxuL/2 Commented Oct 28, 2015 at 8:20

3 Answers 3

1

You can just loop over the array and assign a new Object to each of the items , like this:

for (var i = 0, l = obj_arr.length; i < l; i++) {
    obj_arr[i] = {};
}

UPDATE

You can also do it in this way by applying properties to the global object, for example window:

var people = [ "object_bruno", "object_carlos", "object_luci"];

for (var i = 0, l = people.length; i < l; i++) {
    global[people[i]] = {};
}

Using this solution makes the objects global, so you can use them like object_bruno.

Another improvement can be the usage of computed propertiey names of ECMAScript 2015:

var people = [ "bruno", "carlos", "luci"], prefix = 'object_';

for (var i = 0, l = people.length; i < l; i++) {
    global[prefix + people[i]] = {};
}

This allows to have more meaningful array.

Note, the global can be the window object in browsers or global object in NodeJS, or perhaps something else in other environments.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for Your quick answer! Your code lead into "ReferenceError: object_bruno is not defined" i tried as follows: jsfiddle.net/krL4fxuL
Try the updated solution, with global replaced with window.
your are allready close!!: "" ReferenceError: global is not defined "" when using global
I've provided the fiddle above, also I have a note in the solution at the end. Please read it in detail.
0

Because you are creating a new variable by declaring

obj_arr.forEach(function(func_name) {
    alert(func_name);       //  right names
    var func_name = new Object();   //  no objects are created ???
});

try this

obj_arr.forEach(function(func_name) {
        alert(func_name);       //  right names
        func_name = new Object();   //  no objects are created ???
    });

Comments

0
var obj_arr = [ "object_bruno", "object_carlos", "object_luci"];

obj_arr.forEach(function(func_name, index, arr) { 
    arr[index] = {}; 
});

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.