0
var John = { Cats: 2, Dogs: 3, Turtles: 1 };

var Mary = { Dogs: 0, Parakeets: 3};

How do I append new dimensions after I've already created the objects?

...John now also has 1 Parakeet

...Mary now also has 5 Koi

Rubén
  • 34,714
  • 9
  • 70
  • 166
Suzanne
  • 582
  • 2
  • 11
  • 31
  • 2
    Did you try `John.Parakeet = 1 ` – Ajay Narain Mathur Nov 22 '16 at 03:54
  • 2
    Cats, Dogs, Turtles etc aren't `dimensions`, they are properties – Jaromanda X Nov 22 '16 at 03:55
  • This question could be answered fairly easy through a Google search, but to do that requires that you know the right terminology, which I know is not always easy. First, properties on objects are not called "elements" (that's for arrays), or "dimensions"; they're called **properties**. Second, this is not a multi-dimensional object--I don't even know what that would mean, unless you mean "nested object", which is not what you have here. Once you know the terminology, which any basic JavaScript tutorial can help you out with, you can google for "add property to javascript object". –  Nov 22 '16 at 06:48
  • Yes, thank you for your patience. I am still learning the appropriate nomenclature to search answers more intelligently. – Suzanne Dec 01 '16 at 17:37

1 Answers1

3

Just give them a value, like:

John.Parakeet = 1;

You can then later access these properties just like any other properties.

It really isn't hard.

Matthew
  • 378
  • 2
  • 7
  • 1
    Alternately, `John['Parakeet'] = 1` if you need to use a string (i.e. if you don't know at the time when you write code what the property name will be). – Amadan Nov 22 '16 at 05:16