Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to compare object.key in array in loop

In Table user can select any number of rows to upload on "upload API" but in those selected Rows any one of the row contains status as "NOT OK" I need to show popup that there is "not ok" status please "deselect" that row, if the rows contains status OK then I have to call "upload API".

onUpload(){
  let selectedRows=this.agGrid.api.getSelectedRows();
  selectedRows.forEach((row:any,i:any)=>{
 if( row.status==='OK'){
  /** upload API CALL **/
    }else{
     
     this.isTooltipUpload=true;
  setTimeout(()=>{
  this.isTooltipUpload=false;
},2000)
    }
  })
}

selectedRows :[
{athlete: 'Michael Phelps', age: 23, status:'OK', year: 2008, date: '24/08/2008', …}

{athlete: 'Michael Phelps', age: 19, status:'OK', year: 2004, date: '29/08/2004', …}

{athlete: 'Michael Phelps', age: 27, status:'Not ok', year: 2012, date: '12/08/2012', …}
]

here I am looping the selectedRows using forEach method and comparing the row.status===’OK’, if it is true I need to Call API else need to show popup(this.isTooltipUpload=true).

what I am getting:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

in selectedRows even status is ‘NOT OK’ "if"(if condition false) condition is calling the API and else calling the popup

can any one please help me where I’m doing wrong.

>Solution :

Currently, your code considers each row separately, checking if each individual one is valid. You directly make an API call when the current row is valid, without having ensured that every single row is valid beforehand.

You can use Array#some to check if any row is not valid before attempting API calls.

if (selectedRows.some(r => r.status !== 'OK')) {
    // show error popup
} else {
    // loop over selectedRows and make each API call
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading