JavaScript get total function I have two JS functions that return a total number for me based on some properties. It works fine but feels a bit 'clunky'. Would there be a cleaner way of doing this?

getTotalTrucks: function (data) {
    var noOfTrucks = 0;

    if (data.truck1 == "1") {
        noOfTrucks += 1;
    }
    if (data.truck2 == "1") {
        noOfTrucks += 1;
    }
    if (data.truck3 == "1") {
        noOfTrucks += 1;
    }
    if (data.truck4 == "1") {
        noOfTrucks += 1;
    }
    if (data.truck5 == "1") {
        noOfTrucks += 1;
    }

    return noOfTrucks ;
},

getTotalCars: function (data) {
    var noOfCars = 0;

    if (data.car1 == "2") {
        noOfCars += 1;
    }
    if (data.car2 == "2") {
        noOfCars += 1;
    }
    if (data.car3 == "2") {
        noOfCars += 1;
    }
    if (data.car4 == "2") {
        noOfCars += 1;
    }
    if (data.car5 == "2") {
        noOfCars += 1;
    }

    return noOfCars ;
}
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
    ]
}