Name in the index of an array

Asked

Viewed 54 times

1

I need to create an array or object, but with dynamic name/index, like this of the example, but the name must come from a variable:

var1 = "testeIndex";

var obj = {
    var1 : {
        'elemento1' : 'dado1',
        'elemento2' : 'dado2'
    },
    var2 : {
        'elemento1' : 'dado1',
        'elemento2' : 'dado2'
    },
    var3 : {
        'elemento1' : 'dado1',
        'elemento2' : 'dado2'
    },
};

However, when I put the variable name, even without quotation marks, the index takes the literal name of the variable.

It’s possible to do what I’m trying to do?

1 answer

4


Browser other questions tagged

You are not signed in. Login or sign up in order to post.