api.get(`get-participant-reports/${this.userData.ind_id}`)
.then((res) => {
this.reportData = res.data;
for (var i = 0; i < this.reportData.length; i ) {
api.get(`get-not-eligible360/${this.reportData[i].id}`).then((res) => {
this.reportData.push(res.data)
console.log(this.reportData)
});
}
});
I have 2 api
that I want to populate the same variable
so I use .push()
to insert the 2nd API call to the same variable
.
This is what it looks like:
0:{
"id": 30,
"survey_template_name": "Big 5 Survey",
"report_template_name": "5 Step Profile Report",
"suborg_name": "Sandbox",
"program_name": "MNET Testing",
"iteration_name": "MNET2022 - Test July 2022",
}
1:{
"id": 3280,
"survey_template_name": "Big 5 Survey",
"report_template_name": "5 Step Profile Report",
"suborg_name": "Sandbox",
"program_name": "MNET Testing",
"iteration_name": "iteration-1a",
}
2:{
"0": {
"id": 30,
"not_eligible": "1",
}
}
3:{
"0": {
"id": 3280,
"not_eligible": "1",
}
}
What I want to happen is to get the not_eligible
then push
it into the existing variable
that has the same id
respectively. How can I do that?
CodePudding user response:
I'm assuming that you have response data as array of object for both and need to add key not_eligible
in first response data based on id of first response data so i have update code it should work for you.
api.get(`get-participant-reports/${this.userData.ind_id}`)
.then((res) => {
this.reportData = res.data;
for (var i = 0; i < this.reportData.length; i ) {
api.get(`get-not-eligible360/${this.reportData[i].id}`).then((res) => {
this.reportData.forEach(reportItem => {
const existData = res.data.find(resItem => resItem.id === reportItem.id);
if (existData) {
reportItem.not_eligible = existData.not_eligible
}
});
console.log(this.reportData)
});
}
});
CodePudding user response:
Try with findIndex and if found set object property:
api.get(`get-participant-reports/${this.userData.ind_id}`)
.then((res) => {
this.reportData = res.data;
for (var i = 0; i < this.reportData.length; i ) {
api.get(`get-not-eligible360/${this.reportData[i].id}`).then((res) => {
let idx = this.reportData.findIndex(i => i.id == res['0'].id)
if(idx > -1) this.reportData[idx].not_eligible = res['0'].not_eligible
});
}
});