When to use arrays within JavaScript objects (or mix them)?

orszaczky

Very often I see data structures like this:

var settings = {
    languages: [
        {
            language: 'English',
            translation: 'English',
            langCode: 'en',
            flagCode: 'us'
        },
        {
            ...
        }
    ]
};

Or this:

var settings = {
    languages: [
        {
            'en' : {
                language: 'English',
                translation: 'English',
                flagCode: 'us'
            }
        },
        {
            ...
        }
    ]
};

And this can go many levels deep (within an object there are often other arrays containing further objects)...

Adding arrays brings in another level of complexity when arrays have to be looped through to find a certain object, if we don't know its position in the array. But even if know its position it's still more complicated to use than using purely nested objects, where everything can easily be referred to, using dot notation. Like in this case:

var settings = {
    languages: {
        'en' : {
            language: 'English',
            translation: 'English',
            flagCode: 'us'
        },
        'de' : {
            ...
        }
    }
};

So when is it a good idea to use arrays within objects and when not?

Thank you

My simple answer

  • Use objects ({...}) when you need a collection of key:value pairs

  • Use arrays ([...]) when you need a collection of objects

Other differences

  • arrays are ordered, objects are not

  • arrays are automatically indexed with numbers, objects require you to specify an index

  • arrays have a .length property, objects do not

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Mix objects with objects and arrays to store individual settings and retrieve them

C# objects and arrays, how to use them?

Javascript - Mix two arrays of objects ordering by date and without repetitions

Javascript - How to mix two arrays of objects with specific sorting order?

How to Filter objects and arrays within Javascript Array

create a matrix of objects with x y coordinates within arrays in vanilla JavaScript

Finding the comma, where is it coming from? Javascript Arrays within Objects

When to use const with objects in JavaScript?

Laravel split() returns a mix of arrays and objects

Storing objects in arrays and then accessing them

Does ArrayList use the hashCode method of added objects when adding them

Javascript querySelector to iterate over matching objects and use .innerHTML on them?

Modify arrays within objects in jq

I need each of the first sentences from these JavaScript arrays to display correctly with the day of the week properly included within them

How can I access classes within arrays and style them using javascript

Is there a rule of thumb for when to use JSON objects instead of arrays with a 'name' key?

how to decide when to use array or objects in javascript

React diplay objects that have lists within them

Vue.js difference between v-model and :value when freezing objects in arrays and then trying to modify them

How to resolve javascript objects pushed in to individual arrays instead of all of them being pushed into a single array?

cluster Objects of Arrays (JavaScript)

Are arrays objects in JavaScript?

Javascript "Set" for Objects and Arrays

Comparing Arrays of Objects in JavaScript

Javascript Arrays and Objects

refactorung arrays and objects in javascript

Accessing JavaScript objects in arrays

Looping Through JSON objects and converting them to arrays

I want to create arrays within arrays and enter data in them