JavaScript Trying to get some information with a For function

I am working on an Angular project where I want to catch all the encCharge to add them to give a total of all the encCharge in my cf's with a for function.

This is my array :

produits = [
    {
      cf1: {
        cfTitle: 'prInfiniti30j',
        encCharge: 12345.75,
        encBonifie: 47730.56
      },
      cf2: {
        cfTitle: 'prInfiniti30j',
        encCharge: 18400.94,
        encBonifie: 38268.56
      },
      cf3: {
        cfTitle: 'prInfiniti30j',
        encCharge: 18392.00,
        encBonifie: 30570.56
      },
      cf4: {
        cfTitle: 'prInfiniti30j',
        encCharge: 0.00,
        encBonifie: 15230.56
      },
    },
  ];

This is my for function and the things that I tried, but I can't get to what I am looking for.

ngOnInit(){
for (let testing in this.produits[0]) {
  console.log(this.produits[0].cf1.encBonifie);
  // console.log(this.produits[0].testing.encCharge);
  // console.log(testing.encCharge);
}`   

This is the resultat that I expect :

let totalEncCharge = this.produits[0].cf1.encCharge + 
this.produits[0].cf2.encCharge + this.produits[0].cf3.encCharge + 
this.produits[0].cf4.encCharge ;
console.log(totalEncCharge);

Thank you for your help

Answer:1

You can also try using Object.keys

var total = 0;
Object.keys(this.produits[0]).map(item=>{
  total+=this.produits[0][item].encCharge });
console.log(total);

*Edit: If you do not want any value returned but instead set some other attributes as in the example it is better to use forEach instead of map

Answer:2

The following should work via Object.values() to get the values from the inner object first. After that you can use reduce to simply sum them together.

var produits = [{cf1:{cfTitle:'prInfiniti30j',encCharge:12345.75,encBonifie:47730.56},cf2:{cfTitle:'prInfiniti30j',encCharge:18400.94,encBonifie:38268.56},cf3:{cfTitle:'prInfiniti30j',encCharge:18392.00,encBonifie:30570.56},cf4:{cfTitle:'prInfiniti30j',encCharge:0.00,encBonifie:15230.56},},]

let totalEncCharge = Object.values(produits[0]).reduce((a,c) => a += c.encCharge, 0)

console.log(totalEncCharge);
Answer:3

Result shows "n.split is not a function" unless i include n=n+" " the following code.What does third line mean? function reverse_a_number(n) { n = n + ""; return n.split("").reverse()....

Result shows "n.split is not a function" unless i include n=n+" " the following code.What does third line mean? function reverse_a_number(n) { n = n + ""; return n.split("").reverse()....

  1. write javascript function in html
  2. write javascript function in typescript
  3. write javascript function in js file
  4. write javascript function in angular 6
  5. write javascript function in html tag
  6. write javascript function in ts file
  7. write javascript function in onclick
  8. write javascript function in console
  9. write javascript function inside onclick
  10. write javascript function in code behind c#
  11. write javascript function
  12. write javascript function in php
  13. write javascript function in react
  14. write javascript function inside html
  15. write javascript function with parameter
  16. write javascript function in c#
  17. write javascript function with callback
  18. write javascript function with return value
  19. write javascript function for add(3)(4)
  20. write javascript function in href

I have an AngularJS Application I am trying to post a message through. I am successfully able to log the user in, get the access token, and I have ensured I have my domain in the JavaScript Origins ...

I have an AngularJS Application I am trying to post a message through. I am successfully able to log the user in, get the access token, and I have ensured I have my domain in the JavaScript Origins ...

  1. cors error when calling api
  2. cors error when calling api from angular
  3. cors error when using fetch
  4. cors error in angular
  5. cors error in chrome
  6. cors error in angular 6
  7. cors error in react
  8. cors error in angular 7
  9. cors error in node js
  10. cors error on localhost
  11. cors error on post request
  12. cors error in spring boot
  13. cors error in ajax
  14. cors error on same domain
  15. cors error in react js
  16. cors error in javascript
  17. cors error in angularjs
  18. cors error in angular 8
  19. cors error in web api
  20. cors error in angular 5

I am working with parse.com and looking at making my app secure. I think I have understood well the basics principles of the ACL, CLP, and Cloud functions. My main question comes from the Part IV of ...

I am working with parse.com and looking at making my app secure. I think I have understood well the basics principles of the ACL, CLP, and Cloud functions. My main question comes from the Part IV of ...

  1. security with cloud computing
  2. security with cloud service
  3. security camera with cloud storage
  4. security issues with cloud computing
  5. security concerns with cloud computing
  6. security issues with cloud storage
  7. security risks with cloud computing
  8. security camera with cloud
  9. security issues with cloud
  10. security challenges with cloud computing
  11. cyber security with cloud computing
  12. security threats with cloud computing
  13. security concerns with cloud
  14. data security with cloud computing
  15. security issues with cloud computing pdf
  16. security camera with cloud service
  17. security issues with cloud application
  18. security problems with cloud
  19. security cloud
  20. security cloud symantec

I have a method for validating a string, I want that method to return a Promise as the validations being ran may be asynchronous. The issue I am having however is one of performance, I want the ...

I have a method for validating a string, I want that method to return a Promise as the validations being ran may be asynchronous. The issue I am having however is one of performance, I want the ...