-2

I am trying to calculate row and column total for the following dataset. I am using Jsreport

    "incidentTypes": {
        "Blo": [0,0,1,0,0,0,1,0,0],
        "Ma": [0,0,0,1,0,1,2,0,1],
        "Ph": [1,1,1,0,1,0,0,1,1]
    }

This is the helper function i used to calculate the row total but I am not sure how to calculate the row total

function total(input) {
    var sum = input.reduce((acc,value) => acc+value)
    return sum
}

and this how i called in in jsreport

  {{#each incidentTypes}} 
  <tr>
    <td>{{@key}} </td>
    {{#each this}}
    <td>{{this}}</td>
    {{/each}}
    <td>{{total this}}</td>
 </tr>

1 Answers1

-1

If the rows are each key of incidentTypes, you just have to count how many keys are in it.

function getTotalRows(incidentTypes) {
    return Object.keys(incidentTypes).length
}

Based on: How to efficiently count the number of keys/properties of an object in JavaScript