Javascript 需要在child下动态创建一个child
尝试在Json中的特定子项下添加子项Javascript 需要在child下动态创建一个child,javascript,angular,typescript,Javascript,Angular,Typescript,尝试在Json中的特定子项下添加子项 myObj:any[] = []; this.myObj = { "type": "object", "properties": { "first_name": { "type": "string" }, "last_name": { "type": "string" }, } } addField(){ this.myObj.properties.push({'email':{'ty
myObj:any[] = [];
this.myObj = {
"type": "object",
"properties": {
"first_name": {
"type": "string"
},
"last_name": {
"type": "string"
},
}
}
addField(){
this.myObj.properties.push({'email':{'type': 'string'}}); // its not adding
}
错误类型错误:this.yourJsonSchema.properties.push不是一个函数您不能push()
到一个对象上,该对象用于数组。相反,只需直接设置其属性:
this.myObj.properties.email = { type: 'string' };
编辑:如果需要基于变量设置属性,可以使用[]
语法:
addStringField(field) {
this.myObj.properties[field] = { type: 'string' };
}
不能将()推送到对象上,该对象用于数组。相反,只需直接设置其属性:
this.myObj.properties.email = { type: 'string' };
编辑:如果需要基于变量设置属性,可以使用[]
语法:
addStringField(field) {
this.myObj.properties[field] = { type: 'string' };
}
谢谢,如果我想让电子邮件文本动态化。谢谢,如果我想让电子邮件文本动态化。?