I think I have a problem understanding how javascript or more precisely vuejs in this case works...
the following code is given:
....
data() {
campaigns: [{id: 1, name: 'Campaign 1'}, {id: 2, name: 'Campaign 1'}]
campaignsWithEventSettings: [0, 1] //these are the indexes for campaigns
},
methods: {
saveOrUpdate() {
let campaigns = [];
this.campaignsWithEventSettings.map(x => {
let campaign = this.campaignsSettings[x];
campaign.event_settings = true;
console.log(this.campaigns[x].id) //screenshot with the result is attached below
campaign.id = this.campaigns[x].id;
campaigns.push(campaign);
});
//that gives the same result
/*for (let i = 0; i < this.campaignsWithEventSettings.length; i++) {
let idx = this.campaignsWithEventSettings[i];
let campaign = this.campaignsSettings[idx];
campaign.event_settings = true;
campaign.id = this.campaigns[idx].id;
campaigns.push(campaign);
}*/
let settings = [];
settings.push({eventId: this.eventId});
settings.push({campaigns: campaigns});
axios.post('/events/campaigns', settings).then(resp => {
console.log(resp)
})
},
}
....
the problem is that in the end, all campaigns have the same id, although when running console.log the log ids are different/correct. so in other words at each loop, all campaigns in the array receive a new id (the last one).
console.log screenshot_console
request screenshot_request_data