samedi 27 mars 2021

If-Else condition not getting detected in Google Apps Script

I am looping over the list "res_1" and when the Id is "400" then need to multiply "Total_Weight" with variable "cost_400"; else if the id is "400W", then need to multiply "Total_Weight" with variable "cost_400W". In the end, "result" array should contain "Vendor" and correspoding number ("Total_Weight" * "cost_400").

In the code below, I loop over "res_1", but for some reason, the if condition is not getting detected and it does not go inside the corresponding if or else if condition. Any suggestions would be appreciated.

Expected result:

result = [['ABC',42341820 ],['DEF',91734000]]

Input:

res_1:

[ { Id: '400 ', Vendor: 'ABC', Total_Weight: 32322 },
  { Id: '400W ', Vendor: 'DEF', Total_Weight: 61156 } ]

var cost_400 = 1310
var cost_400W = 1500

res_1.forEach((r2,i2)=>{
  if (r2['Id'] == "400" ) {
    Logger.log(r2['Total_Weight']*cost_400)
  }
  else if (r2['Id'] == "400W" ) {
    Logger.log(r2['Total_Weight']*cost_400W)
  }
});

Aucun commentaire:

Enregistrer un commentaire