[Vuejs]-How to pick an specific object then push it into an existing array?

1๐Ÿ‘

โœ…

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)
              });
            }
          });

1๐Ÿ‘

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
          });
        }
      });

Leave a comment