Is there a more elegant way to iterate over json array in js?

I am trying to iterate over a json array and calculate number of hours from every object. The code works but it just feels like there could be a more efficient way to write the code, but whatever I try the code just breaks.

My json array:

{
    "timesheetItems":[
        {
        "id":"1",
        "title":"Ticket system integration",
        "hours":2
        },
        {
        "id":"2",
        "title":"Integration with Google Maps API",
        "hours":3
        },
        {
        "id":"3",
        "title":"Prepare test cases",
        "hours":4
        }
    ]
}

Iteration:

let numberOfSheets = 0;
const lam = this.state.sheets.forEach( item => numberOfSheets += item.hours)

10 thoughts on “Is there a more elegant way to iterate over json array in js?”

Leave a Comment