JavaScript get total function
Magic values
Your code compares to &quot;1&quot; and &quot;2&quot;. What are these values supposed to convey? Use constants to signify what they mean.
Data structure
Your data structure is not great for the task you are trying to accomplish. Why not use a list containing car types? Why not at least split the data based on cars and trucks, so you can get away with just using Object.keys(data.cars).length or even a simple reduce?
{
    &quot;cars&quot; : {
         &quot;car1&quot;: &quot;2&quot;,
         &quot;car2&quot;: &quot;0&quot;
    },
    &quot;trucks&quot; : {
         &quot;truck1&quot;: &quot;0&quot;,
         &quot;truck2&quot;: &quot;1&quot;
    }
}

or
{
    &quot;cars&quot;: [
        {
            &quot;type&quot;: &quot;car1&quot;,
            &quot;status&quot;: &quot;broken&quot;
        },
        {
            &quot;type&quot;: &quot;car2&quot;,
            &quot;status&quot;: &quot;contains 4 wheels&quot;
        }
    ],
    &quot;trucks&quot;: [
        //you get the point
    ]
}