javascript 迭代/循环通过JSON对象

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 迭代/循环通过JSON对象相关的知识,希望对你有一定的参考价值。

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);

以上是关于javascript 迭代/循环通过JSON对象的主要内容,如果未能解决你的问题,请参考以下文章