-7
const monsters = {
  '1': {
    name: 'godzilla',
    age: 250000000
  },
  '2': {
    Name: 'manticore',
    age: 21
  }
}

I learn JavaScript from Codecademy, What does this code mean?
Is this two dimensional array? If not, what is it?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • It is two JS objects with properties inside a third outer JS object. If you consider objects as associative arrays or Hashes or maps, then the outer object has a key 1 and a key 2 with objects as values. – eckes Feb 10 '19 at 04:29

1 Answers1

3

The data structure you are showing in your code example is not an array at all, it is an object. Arrays are defined using square brackets ([]) and their keys (indices) are not explicitly declared but rather assigned automatically.

So if you wrote your code like this, for example, you would have an array containing objects:

const monsters = [
  {
    name: 'godzilla',
    age: 250000000
  },
  {
    name: 'manticore',
    age: 21
  }
]

…so you could access the values by their array index, like so.

monsters[0].name; // godzilla
monsters[1].name; // manticore
Patrick Hund
  • 19,163
  • 11
  • 66
  • 95