0

I'm quite new to JavaScript and programming in general and figured I'd hone my abilities by working on a small project. The idea is that I have a form for information on an event, an input for the name, date, time and a small thumbnail image.

I want each event to be an object inside of an array, so I would have something like:

var concerts = {};

for (var i = 1; i < 11; i++) {
    window["concert"+i] = new Object();
}

and the array would end up being something:

var concerts = [concert1, concert2, concert3]

and so on.

How could I get this loop to work so that it would take the 3 parameters and create a new object in the array named 'concert'+i? Thanks for reading!

1
  • 2
    Take 3 parameters from where? Commented May 8, 2017 at 10:56

2 Answers 2

3

Concerts must be an array:

var concerts = [];

for (var i = 0; i < 10; i++) {
  concerts[i] = {
    //maybe also giveit a name if you want to:
    name:"concert"+i
  };
}

You can access it like this:

concerts[0].name="Wacken";//first concert...

Note that this:

window["concert"+i] = new Object();

is very bad style...

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

Comments

0

First you declare a variable concerts of type object. But you want an array. That first line makes your code very confusing.

You have to start with an empty array:

var concerts = []; // alternatively: new Array();

In the end you'd like to have a structure like this:

[
    { /* object 1 */ },
    { /* object 2 */ }
]

Now you can run a foor-loop and populate the array:

for (var i = 0; i <= 10; i++) {
    concerts.push({['concert' + i]: {}});
}

This will return something like:

[
    {'concert0': {}},
    {'concert1': {}},
    // skipped
    {'concert10': {}}
]

Later you can populate the objects. This is however not a very nice style. Maybe you should reconsider if it is really necessary to give a name to every object like concert0...10.

Go rather for:

var concerts = [
    {
        'name': 'concert0',
        'location': 'somewhere'
    }
    // continue with other objects
];

3 Comments

@Jonasw Thanks for the comment! Array can't be a key in a JavaScript object. This expression however is not an array. The part ['concert' + i] returns a string - 'concert' is string and i is number. Due to the type coercion in JS the result is a string, returned by the square brackets. Otherwise you couldn't create dynamically the key names in a foor loop.
oh sorry, ive already forgot that... :/ maybe because i think that dynamic keys are a generally bad idea...
I think, too, that dynamic keys are a very bad idea. It's what the OP wanted. In JavaScript you can call a value of an object with a dot notation or with square brackets: var o.name = 'Jonas'; or var o['name'] = 'Jonas';. The key in the square brackets can be generated by some operation: var o['n' + 'am' + 'e'] = 'Jonas';. In my answer it looks really weird, that is why I suggest in the end to go for another solution.

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.