0

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!

3
  • 1
    intent.messages = []? I'd strongly recommend reviewing basic JS before trying to stack TypeScript and the Angular APIs on top of it. Commented Jan 14, 2021 at 22:43
  • Thankyou, I will do that! Commented Jan 14, 2021 at 22:46
  • intent.messages = [] //it may through compile error in angular 'as messages doesn't exist on object ' intent['messages'] = []// error free` Commented Jan 14, 2021 at 23:16

1 Answer 1

1

You can set the value directly to a blank array:

this.intent.messages = [];

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.