How to declare a Multidimensional Array key - Javascript

Asked

Viewed 147 times

0

Ola, I would like to know how to concatenate two keys of an array, the idea would be more or less like this:

dados['azul'][i] = id; 

Where the Data Array has the key that is a predefined color and its second key is an i incremented with the ID value and all this within a for...

Once it enters the loop it will make a case to add the value of the element ID into the correct key. If the expected color is blue, for example, it will add a new value to the array, however several other values will be added as the loop runs.

The result would be something like this:

dados[azul]{
  [2] = valordeID;
  [1] = valordeID;
}
dados[preto]{
  [2] = valordeID;
  [1] = valordeID;
}

And so on, some suggestion?

1 answer

1

If what you want is an Object that holds a Value and has a Key, I believe you can use one Map .


var myMap = new Map();
myMap.set('azul', seuArray);

  • Actually I think that would not work, because the idea is to add several values to a color and not a ready array.

Browser other questions tagged

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