即時創建一個循環來創建對象。循環返回始終是最後一個值
Action.ts
public campaing:any = {
'id': '',
'campaing_code': '',
'campaing_type': '',
'start_date': this.currentDate,
'end_date': this.currentDate,
'deadline': this.currentDate,
'creation_date': this.currentDate,
'special': false,
'os': '',
'country': '',
'campaing_country': 'germany',
'version_app': '',
'permanent_promo': false,
'promo_tag': 'ABS-W-',
'editor_name': '',
'plus_promotag': '',
'status': 'Successful',
'application': {},
'publisher': {},
'contact': '',
'sended': false
};
public searchparram: any = {
'type': '',
'time': '',
'start_date': this.currentDate,
'deadline': this.currentDate,
'publisher': {},
'wildcard': false,
'os': '',
'category': '',
'number_campaings': 1
}
public suggescampaings:any = [];
public generateCampaings(){
this.campaing.campaing_code = this.searchparram.type;
this.campaing.start_date = this.searchparram.start_date;
this.campaing.deadline = this.searchparram.deadline;
this.campaing.publisher = this.searchparram.publisher;
this.campaing.os = this.searchparram.os;
for (let i = 1; i <= this.searchparram.number_campaings; i++) {
((i)=>{
this.campaing.id = i; /* Here should print i but alway print the last value of i */
this.suggescampaings.push(this.campaing);
})(i);
}
}
但是當我嘗試把camaping.id =我,總是返回迭代的最後一個值。我的意思是,如果iteraion是8次,總是給id 8。
所以這個想法是把id作爲迭代,然後推入數組中的對象。
因爲你改變了同一個對象('this') – Andreas
當然只會有最後的值。在每個循環中覆蓋'this.campaing.id'。你期望什麼? – Saravana