Home > Enterprise >  Why is this forEach code snippet invalid in AngularJS?
Why is this forEach code snippet invalid in AngularJS?

Time:04-09

Here I have a generic calculation function.

private calculateTotal(order: InventoryOrder):number{
  let total = 0.0;
  if(order && order.currentInventory){
    order.currentInventory.forEach(x =>{
      console.log(x.quantity);
      console.log("helloworld");
    }  //error appears here<- "," expected.
  } return total;
}

I tested very similar code at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

    const array1 = ['a', 'b', 'c'];
    
    array1.forEach(element => {
                    console.log(element);
                    console.log("helloworld");
                              });

// expected output: "a"
// expected output: "b"
// expected output: "c"

Any idea what's going on?

CodePudding user response:

private calculateTotal(order: InventoryOrder):number{
    let total = 0.0;
    if(order && order.currentInventory){
        order.currentInventory.forEach(x =>{
            console.log(x.quantity);
            console.log("helloworld");
        });
    } 
    return total;
}
  • Related