如何在Angular(typescript)中向JSON对象添加新的键数组?
我已经尝试寻找解决方案,但我发现最接近的方法是添加一个带有值的简单键 我有一个Json如何在Angular(typescript)中向JSON对象添加新的键数组?,json,angular,typescript,Json,Angular,Typescript,我已经尝试寻找解决方案,但我发现最接近的方法是添加一个带有值的简单键 我有一个Json对象: { "answers": [ { "phrase": "Why?", "nextIntentId": "4a10fd2b-bc6b-4563-8d97-34ef6dec1ba9" }, { "phras
对象
:
{
"answers": [
{
"phrase": "Why?",
"nextIntentId": "4a10fd2b-bc6b-4563-8d97-34ef6dec1ba9"
},
{
"phrase": "Faster recovery?",
"nextIntentId": "4f9eeecf-6477-435b-9f1b-43304748a4e5"
}
],
"intentId": "19f458e8-ce1f-43eb-beb8-b8ef4e7ff4c7"
}
我想向这个对象添加一个数组:
“消息”:[]
这个想法是,之后我可以intent.messages.push(object)
对象到这个数组,就像中的“answers”:[]
数组一样
{
"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
}
{
“答案”:[
{
“短语”:“为什么?”,
“NEXTINENTID”:“4a10fd2b-bc6b-4563-8d97-34ef6dec1ba9”
},
{
“短语”:“更快的恢复?”,
“NEXTINENTID”:“4f9eeecf-6477-435b-9f1b-43304748a4e5”
}
],
“意向书”:“19f458e8-ce1f-43eb-beb8-b8ef4e7ff4c7”,
“消息”:[]您可以直接将值设置为空白数组:
this.intent.messages=[];
intent.messages=[]
?我强烈建议在尝试将TypeScript和Angular API堆叠在其上之前先查看基本JS。谢谢,我会这样做的!intent.messages=[]//这可能是由于Angular中的编译错误“因为对象上不存在消息”
intent['messages']=[]
//无错误`