JavaScript Object Literal Array Examples

Sam Deering
Share

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.