JavaScript Adding key/values pairs to JSON map down one level adding key values to dictionary python,adding key values to

I'm trying to dynamically add new key/value pairs to an existing json map using jquery/javascript. An example of my structure is as follows:

var widgetTypes = {
  "CLASS_A": {
    "A1": "A1 Description",
    "A2": "A2 Description"
  },
  "CLASS_B": {
    "B1": "B1 Description",
    "B2": "B2 Description"
  }
};

How do I append a key/value pair to either class (i.e. CLASS_A or CLASS_B). For example, how would I add item A3 with description "A3 Description" to the CLASS_A section of the structure so that I then had the following?

var widgetTypes = {
  "CLASS_A": {
    "A1": "A1 Description",
    "A2": "A2 Description"
    "A3": "A3 Description"
  },
  "CLASS_B": {
    "B1": "B1 Description",
    "B2": "B2 Description"
  }
};

I know you can do something like widgetTypes['CLASS_A'].type = 'A3' etc. but I can't seem to get it quite right (or I'm way off the track). I found a lot of examples of adding items the level above (i.e. adding a CLASS_C) but nothing for what I'm interested in doing.

Answer:1

There are several ways:

  1. . Dot notation.
  2. [] Bracket notation.
  3. Object.defineProperty() method.

var widgetTypes = {
  "CLASS_A": {
    "A1": "A1 Description",
    "A2": "A2 Description"
  },
  "CLASS_B": {
    "B1": "B1 Description",
    "B2": "B2 Description"
  }
};

1.

widgetTypes.CLASS_A.A3 = "A3 Description"

2.

widgetTypes["CLASS_A"]["A3"] = "A3 Description"

3.

Object.defineProperty(widgetTypes.CLASS_A, 'A3', {
  value: "A3 Description",
  writable: true,
  enumerable: true,
  configurable: true
});

Option 3 has several other advantages over the two that you can configure the property behavior.

Answer:2

I've a written a JavaScript that opens a specific page in the "content" div when I click on a link. HTML: <li><a href='#' onclick="load_shift_test()">Soft Drinks</a></li> JS:...

I've a written a JavaScript that opens a specific page in the "content" div when I click on a link. HTML: <li><a href='#' onclick="load_shift_test()">Soft Drinks</a></li> JS:...

I'm using Select2 Plugin in my asp.net mvc 5 application. according to the documentation The placeholder option allows you to pass in a data object instead of just a string if you need more ...

I'm using Select2 Plugin in my asp.net mvc 5 application. according to the documentation The placeholder option allows you to pass in a data object instead of just a string if you need more ...

  1. select2 placeholder not displaying
  2. select2 placeholder not showing

I'm trying to shift an image around based on the device orientation. But the values are very jumpy at rest, going between -1 to +2 with no movement, and I need a way to smooth it out a bit. Is there ...

I'm trying to shift an image around based on the device orientation. But the values are very jumpy at rest, going between -1 to +2 with no movement, and I need a way to smooth it out a bit. Is there ...

I've used jQuery version 2.2.4 and tried to capture event - no luck. Is there any way to fix issue? This code works: window.addEventListener('test.namespace', e => console.log('CustomEvent with ...

I've used jQuery version 2.2.4 and tried to capture event - no luck. Is there any way to fix issue? This code works: window.addEventListener('test.namespace', e => console.log('CustomEvent with ...

  1. dispatch custom event with data