Skip to content
Advertisement

Can I create an object without a name in JS?

I have a structure that looks like this:

var set_2 =  {
    nameofSet : 'French greetings',
    category: 'languages',
    cards : [
        {
            front : 'bonjour',
            back : 'dzien dobry'
        },
        {
            front : 'bonne nuit',
            back : 'dobranoc'
        },
        {
            front : 'bon soir',
            back : 'dobry wieczor'
        }
    ]
}

I can iterate over them in a loop like this:

 var cards = set_2.cards;
 for (k = 0;k<cards.length;k++) {
     var frontSide = cards[k].front;
     var backSide = cards[k].back;
 }

Do I assume correctly that in the cards array I have a couple of objects without names?

If so, how can I push more objects like that to the cards array without giving them names? I want to create those objects in a for loop.

Advertisement

Answer

I’m not sure what you mean by names, but you can push more objects into the array as such:

set_2.cards.push({front: 'front', back: 'back'});
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement