我有一個REST API,它包含JSON格式的數據。我將它存儲在一個對象數組中。但是我想爲每個對象附加一個NEW EMPTY數組。我無法做到。 以下是我的REST API的外觀。我在註釋中標記了我想要添加的每個對象的新數組。typescript angular2:如何將數組追加到數組中的每個對象?
content = [
{
text: 'abc',
options: [
{
Id: 1,
Text: 'aaa'
},
{
Id: 2,
Text: 'bbb'
},
{
Id: 3,
Text: 'ccc'
}],
// ARRAY[]
},
{
text: 'def',
options: [
{
Id: 21,
Text: 'qwerty'
},
{
Id: 22,
Text: 'zxcv'
},
{
Id: 23,
Text: 'asdf'
}],
// ARRAY[]
}
}]
這是我試過的。
public newarr:Array<any>;
this.httpservice.post('RESTUrl').subscribe(resp=>{
this.contents = resp.data;
this.contents.forEach((x:any)=>{
x.push(this.newarr);
});
console.log("contents",this.contents);
});
非常感謝。它可以工作。但是對於數組,它也是一樣的嗎? –
是的,你也可以添加數組 –
這是相同的方式還是我需要這樣寫:x.arr [] = this.newarr? –