fork download
  1. const dados = [
  2. { id: 1, nome: "Primeiro envio",
  3. estatistica: [
  4. { id: 1, entregues: 0 },
  5. { id: 2, entregues: 2 }
  6. ]
  7. },
  8. { id: 2, nome: "Segundo envio",
  9. estatistica: [
  10. { id: 1, entregues: 1 },
  11. { id: 2, entregues: 3 }
  12. ]
  13. }
  14. ];
  15.  
  16. // for of
  17. let soma = 0;
  18. for (const dado of dados) {
  19. for (const item of dado.estatistica) {
  20. soma += item.entregues;
  21. }
  22. }
  23. console.log(soma);
  24.  
  25. // for tradicional
  26. soma = 0;
  27. for (let i = 0; i < dados.length; i++) {
  28. for (let j = 0; j < dados[i].estatistica.length; j++) {
  29. soma += dados[i].estatistica[j].entregues;
  30. }
  31. }
  32. console.log(soma);
  33.  
  34. // reduce, olha a zona
  35. soma = dados.reduce((x, y) => x + y.estatistica.reduce((a, b) => a + b.entregues, 0), 0);
  36. console.log(soma);
  37.  
Success #stdin #stdout 0.08s 31548KB
stdin
Standard input is empty
stdout
6
6
6