使用JavaScript在jsreport中从JSON数据计算总值。

huangapple go评论73阅读模式
英文:

calculating total values from a JSON data in jsreport using JavaScript

问题

我是jsreport的新手。我有以下数据,尝试计算总薪水,

  1. {
  2. "company": [{
  3. "Remy": {
  4. "age": 32,
  5. "employer": "emp1",
  6. "salary": 20000
  7. },
  8. "Piet": {
  9. "age": 35,
  10. "employer": "emp2",
  11. "salary": 50000
  12. },
  13. "Thando": {
  14. "age": 32,
  15. "employer": "emp3",
  16. "salary": 20000
  17. },
  18. "Greg": {
  19. "age": 33,
  20. "employer": "emp4",
  21. "salary": 70000
  22. }
  23. }]
  24. }

我尝试使用以下代码,但一直出现错误,提示company.forEach不是一个函数。

  1. function total(company) {
  2. var sum = 0
  3. company.forEach(function (i) {
  4. sum += i.salary
  5. })
  6. return sum
  7. }

我得到以下错误。

英文:

I am new to jsreport. I have the following data and trying to calculate total salaries,

  1. {
  2. "company": [{
  3. "Remy": {
  4. "age": 32,
  5. "employer": "emp1",
  6. "salary": 20000
  7. },
  8. "Piet": {
  9. "age": 35,
  10. "employer": "emp2",
  11. "salary": 50000
  12. },
  13. "Thando": {
  14. "age": 32,
  15. "employer": "emp3",
  16. "salary": 20000
  17. },
  18. "Greg": {
  19. "age": 33,
  20. "employer": "emp4",
  21. "salary": 70000
  22. }
  23. }]
  24. }

I tried using the following code but I keep getting an error that company.forEach is not a function

  1. function total(company) {
  2. var sum = 0
  3. company.forEach(function (i) {
  4. sum += i.salary
  5. })
  6. return sum
  7. }

I am getting the following error.

  1. Report "Issue" render failed.
  2. Error when evaluating engine handlebars for template anonymous
  3. (because) "total" helper call failed
  4. (because) company.forEach is not a function
  5. (sandbox.js line 14:13)
  6. 12 | function total(company) {
  7. 13 | var sum = 0
  8. > 14 | company.forEach(function (i) {
  9. | ^
  10. 15 | sum += i.salary
  11. 16 | })
  12. 17 | return sum

答案1

得分: 0

这是一个使用reduce的好时机:

  1. const data = {
  2. "company": [{
  3. "Remy": {
  4. "age": 32,
  5. "employer": "emp1",
  6. "salary": 20000
  7. },
  8. "Piet": {
  9. "age": 35,
  10. "employer": "emp2",
  11. "salary": 50000
  12. },
  13. "Thando": {
  14. "age": 32,
  15. "employer": "emp3",
  16. "salary": 20000
  17. },
  18. "Greg": {
  19. "age": 33,
  20. "employer": "emp4",
  21. "salary": 70000
  22. }
  23. }]
  24. }
  25. const salaries = Object.values(data.company[0]).reduce((total, emp) => {
  26. total = emp.salary + total;
  27. return total;
  28. }, 0)
  29. console.log(salaries)

有关详细信息,请查看数组的reduce方法,但每当你需要在一个项目列表上进行“计算”时,reduce都是一个不错的选择,可以用来尝试解决问题。

英文:

This is a good time to use reduce:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const data = {
  2. &quot;company&quot;: [{
  3. &quot;Remy&quot;: {
  4. &quot;age&quot;: 32,
  5. &quot;employer&quot;: &quot;emp1&quot;,
  6. &quot;salary&quot;: 20000
  7. },
  8. &quot;Piet&quot;: {
  9. &quot;age&quot;: 35,
  10. &quot;employer&quot;: &quot;emp2&quot;,
  11. &quot;salary&quot;: 50000
  12. },
  13. &quot;Thando&quot;: {
  14. &quot;age&quot;: 32,
  15. &quot;employer&quot;: &quot;emp3&quot;,
  16. &quot;salary&quot;: 20000
  17. },
  18. &quot;Greg&quot;: {
  19. &quot;age&quot;: 33,
  20. &quot;employer&quot;: &quot;emp4&quot;,
  21. &quot;salary&quot;: 70000
  22. }
  23. }]
  24. }
  25. const salaries = Object.values(data.company[0]).reduce((total, emp) =&gt; {
  26. total = emp.salary + total;
  27. return total;
  28. }, 0)
  29. console.log(salaries)

<!-- end snippet -->

Have a look at the array reduce method for details, but whenever you hear 'calculating' on a list of items reduce isn't a bad option to have a look at to try to solve the issue

huangapple
  • 本文由 发表于 2023年2月6日 20:58:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361623.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定