EdgarSketches
EdgarSketches

Reputation: 11

How to add a new array of keys to a JSON object in Angular (typescript)?

I have tried looking for a solution, but the closest I cold find were with adding a simple key with a value...

So, I have this Json object:

 {
    "answers": [
      {
        "phrase": "Why?",
        "nextIntentId": "4a10fd2b-bc6b-4563-8d97-34ef6dec1ba9"
      },
      {
        "phrase": "Faster recovery?",
        "nextIntentId": "4f9eeecf-6477-435b-9f1b-43304748a4e5"
      }
    ],
    "intentId": "19f458e8-ce1f-43eb-beb8-b8ef4e7ff4c7"
  }

And I want to add an array to this object: "messages" : []

The idea is, that afterwards I can intent.messages.push(object) objects to this array, just like in the "answers" : [] array

 {
    "answers": [
      {
        "phrase": "Why?",
        "nextIntentId": "4a10fd2b-bc6b-4563-8d97-34ef6dec1ba9"
      },
      {
        "phrase": "Faster recovery?",
        "nextIntentId": "4f9eeecf-6477-435b-9f1b-43304748a4e5"
      }
    ],
    "intentId": "19f458e8-ce1f-43eb-beb8-b8ef4e7ff4c7",
    "messages" : []                            <<------------ I want to add this aray
  }

I have also tried object.push('messages[]') and got Error: this.intent.push is not a function

Thankyou for any kind of help in advance!

Upvotes: 0

Views: 65

Answers (1)

abney317
abney317

Reputation: 8512

You can set the value directly to a blank array:

this.intent.messages = [];

Upvotes: 1

Related Questions