JavaScript
Article

JavaScript Object Literal Array Examples

By Sam Deering

Is it possible to declare arrays in JavaScript object literal notation?

Example 1 – this works with arrays

Declaration:

NAMESPACE = {

    data:
    {
        items: Array() //array
    }

}

Storage of data:

NAMESPACE.data.items.push(data[0]);

Example 2 – this works with objects

Declaration:

NAMESPACE = {

    data:
    {
        items: {} //object
    }

}

Storage of data:

NAMESPACE.data.items[data['key']] = data;

Please share your thoughts on this in comments.

Recommended

Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Get the latest in JavaScript, once a week, for free.