Home > Net >  Alert inside a For Loop
Alert inside a For Loop

Time:02-01

I have 4 types of document status: In Preparation, For Approval, Approved, Rejected. Also, this code has a checkbox that's why the array is called 'selectedPurhcaseRequests'. So the code is every time that I check a checkbox, it get its data and pushes it to the 'selectedPurchaseRequests' array. And inside of that data, it has a document status. So I want to have a for loop to check all the purchase request's document status.

for(let i = 0; i < this.selectedPurchaseRequests.length; i  ) {
    if(this.selectedPurchaseRequests[i].docStatus !== 'In Preparation') {
        continue;
    } else {
        fetch(`url`, {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json'
            }
        })
    }
}

Now, I want to alert a message with a summary of those purchase request number with a status of not equal to 'In Preparation'. But I want it to alert one time when 'selectedPurchaseRequests' array is done looping. Because if it alerts multiple time, it will take a lot time and effort for the user to press ok.

Can someone help me?

CodePudding user response:

You certainly have some other properties to identify each selected purchase requests... Like in the below example, a number and/or a description:

let requestStatuses = [];

for(let i = 0; i < this.selectedPurchaseRequests.length; i  ) {

  requestStatuses.push({
    number: this.selectedPurchaseRequests[i].number,
    description: this.selectedPurchaseRequests[i].description,
    status: this.selectedPurchaseRequests[i].docStatus
  })

  if(this.selectedPurchaseRequests[i].docStatus !== 'In Preparation') {
    continue;
  } else {
    fetch(`url`, {
      method: 'PUT',
      headers: {
        'Content-Type': 'application/json'
      }
    })
  }
}

console.log(requestStatuses)  // do something with that array

CodePudding user response:

Working Demo :

new Vue({
  el: '#app',
  data() {
    return {
        selectedPurchaseRequests: [{
        number: 1,
        summary: 'Aplha',
        docStatus: 'Approved'
      }, {
        number: 2,
        summary: 'Beta',
        docStatus: 'In Preparation'
      }, {
        number: 3,
        summary: 'Gama',
        docStatus: 'Rejected'
      }],
      notEqualInPrep: []
    }
  },
  mounted: function() {
    this.selectedPurchaseRequests.forEach((request) => {
        if (request.docStatus !== 'In Preparation') {
        this.notEqualInPrep.push(`${request.number}-${request.summary}-${request.docStatus}`);
      }
    });
    
    alert(this.notEqualInPrep.toString())
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
</div>

  •  Tags:  
  • Related