RPeraltaJr
11/17/2017 - 12:01 AM

Iterate/Loop through JSON object

function calcAvgHeight(data) {
    // Calculate average height from received data. If no data, return null.
    var total = 0;
    var count = 0;
    var avgHeight = 0;
    if( Object.keys(data).length !== 0 ) { // if length is not 0

        for (var i in data) {
            total += data[i].height;
            count++;
        };

    } else {
        return null;
    }
    avgHeight = total / count;
    return avgHeight;
}

var avgHeight = calcAvgHeight({
    Matt: { height: 174, weight: 69 },
    Jason: { height: 190, weight: 103 }
});

console.log(avgHeight);