Bunker
Bunker

Reputation: 1051

How do you create an object containing an array using literals

I want to do this using literals:

newObject.object[0].time = 1200;

But the following code gives an error:

var newObject = {
    object[0].time: 1200
};

Is there any way to construct such an objects using literals?

Upvotes: 1

Views: 31

Answers (1)

VisioN
VisioN

Reputation: 145398

var newObject = {        // base object
    object: [            // array as "newObject.object"
        {                // object as a first element of array
            time: 1200
        }
    ]
};

console.log(newObject.object[0].time);  // 1200

Upvotes: 3

Related Questions