5

I'm trying to create a new object for each item in an array by looping. The names of the objects should be based on the key of the array.

So for this array:

var arr = new Array(
    "some value",
    "some other value",
    "a third value"
);

Would result in three objects:

alert(object1.value);
alert(object2.value);
alert(object3.value);

The code I have thus far (but isn't working) is:

// Object
function fooBar(value) {
    this.value = value;
    ...
}

// Loop
var len = arr.length;
for (var i = 0; i < len; i++) {
    var objectName = object + i;
    var objectName = new fooBar(arr[i]);
}

Does what I'm asking for even make sense?

1
  • 1
    objectName is declared twice. Commented Jan 5, 2013 at 23:32

4 Answers 4

9

You have to make an array of the objects also

var objs = new Array();

for(var i = 0; i < len; i++) {
  objs[i] = new fooBar(arr[i]);
}

alert(objs[0].value);
Sign up to request clarification or add additional context in comments.

Comments

5

You can map your array:

var arr = new Array(
    "some value",
    "some other value",
    "a third value"
);
var fooBars = arr.map(function(x) { return new fooBar(x); });

Then you can access each value:

alert(fooBars[0].value);
// etc.

or process them all at once:

fooBars.forEach(function (foo) { alert(foo.value); });

Comments

4

What you're asking for makes sense, but shouldn't be how you're building you JavaScript out.

Technically, there is a way of creating vars with names you build dynamically, but you shouldn't use it as it's slow, and if users are specifying what's in the array, it's unsafe and the feature is being needed in a couple of years, so your old stuff might break in future browsers.

Meanwhile, you could easily do something like:

var obj = {},
    arr = [ "one", "two", "three" ],

    i = 0,
    len = arr.length,
    val = "",
    name = "";

for (; i < len; i += 1) {
    name = "item" + i;
    val = arr[i];
    obj[name] = val;
}

Now you can call obj.item1; // "two"

If you're really desperate, you can use window as obj so when you're writing stuff in the global scope, you can just write item0; // "one" but this really isn't a great idea, for several reasons (readability, maintainability, likelihood of overwriting somebody else's properties, etc).

Comments

3

If you really want the variable named so, here's a solution

function fooBar(value) {
    this.value = value;
}

var arr = new Array(
    "some value",
    "some other value",
    "a third value"
);

(function(context) {
    for ( var i = 0; i < arr.length; i++) {
       var key = 'object' + ( i + 1 );
       this[ key ] = new fooBar( arr[ i ] );
     }
}(window));

alert(object1.value);
alert(object2.value);
alert(object3.value);

If you don't want global variables object1 ... just replace the keyword window with this and it will produce local variable to the current scope.

Test it out here: http://jsfiddle.net/bukart/F8ham/1/

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.