Javascript 单击后在Json上创建新对象(http.put)
我有以下json结构:Javascript 单击后在Json上创建新对象(http.put),javascript,json,angular,Javascript,Json,Angular,我有以下json结构: "disputas": [ { id: "", tipo_negociacao: "", historico:{ fl_usuario: "", created_at: "", updated_at: "", created_by: null, updated_by: null, texto: "", }
"disputas": [
{
id: "",
tipo_negociacao: "",
historico:{
fl_usuario: "",
created_at: "",
updated_at: "",
created_by: null,
updated_by: null,
texto: "",
}
}
]
我正在使用http.put更新这个json,我想在每次单击按钮时创建另一个historico对象,所以它看起来像这样:
"disputas": [
{
id: "",
tipo_negociacao: "",
historico:{
fl_usuario: "",
created_at: "",
updated_at: "",
created_by: null,
updated_by: null,
texto: "",
},
historico:{
fl_usuario: "",
created_at: "",
updated_at: "",
created_by: null,
updated_by: null,
texto: "",
}
}
]
我该怎么做
这是我的服务:
url: string = 'http://localhost:3004/disputa';
constructor(private http: Http){}
atualizaDisputa (body:Object): Observable<DisputaPropostaComponent[]>{
let bodyString = JSON.stringify(body); // Stringify payload
let headers = new Headers({ 'Content-Type': 'application/json' }); // ... Set content type to JSON
let options = new RequestOptions({ headers: headers }); // Create a request option
return this.http.put(`${this.url}/${body['id']}`, body, options) // ...using post request
.map((res:Response) => res.json()) // ...and calling .json() on the response to return data
.catch((error:any) => Observable.throw(error.json().error || 'Ocorreu um erro em nosso servidor, tente novamente mais tarde')); //...errors if any
}
提前谢谢。如果你们需要更多代码,请告诉我。对象上不能有两个名为historico的键。您需要将其转换为数组。一旦这样做了,您就可以像往常一样推到阵列上 你的问题不清楚。你想创建这样的东西:historico:{fl_usuario:,created_at:,updated_at:,created_by:null,updated_by:null,texto:,}当按下按钮时?我刚刚编辑了这个问题,我想现在更清楚了,一个物体上不能有两把名为historico的钥匙。您需要将其转换为数组。一旦你这样做了,你就可以像往常一样推到阵列上。哦,是的,没错。对不起,我弄糊涂了。我用数组来做。谢谢