javascript - 数组 - 在平均计算中,javascript排除了一些值

以下是一些数据:


data = [


 {"Age":26,"Level":8},


 {"Age":37,"Level":9},


 {"Age":null,"Level":15},


 {"Age":null,"Level":45}


];



我正在尝试计算属性的平均值:


var avg = {};


 var rows = data.length;


 data.forEach(obj => {


 Object.keys(obj).forEach(k => {


 if(obj[k] != null){


 avg[k] = (avg[k] || 0) + obj[k] / rows;


 }


 });


 });



 return avg;



但问题出在有空值的属性中。15.75而不是31.5,因为数据的长度总是4(并且应该是2)。

时间:

你可以独立存储每个键的总和和计数。


var data = [{"Age": 26,"Level": 8 }, {"Age": 37,"Level": 9 }, {"Age": null,"Level": 15 }, {"Age": null,"Level": 45 }],


 avg = {},


 temp = {};



data.forEach(obj => Object.keys(obj).forEach(k => {


 if (obj[k] === null) return;


 temp[k] = temp[k] || { sum: 0, count: 0 };


 temp[k].sum += obj[k];


 temp[k].count++;


 avg[k] = temp[k].sum / temp[k].count;


}));



console.log(avg);


console.log(temp);
.as-console-wrapper { max-height: 100% !important; top: 0; }

可以有一个有嵌套对象的对象,该对象有两个属性value和count


const data = [


 {"Age":26,"Level":8},


 {"Age":37,"Level":9},


 {"Age":null,"Level":15},


 {"Age":null,"Level":45}


];



let avg = {}



data.forEach(x => {


 for(let k in x){


 if(!avg[k]){


 avg[k] = {value:0,count:0};


 }


 if(x[k] !== null){


 avg[k].value += x[k]


 avg[k].count++;


 }


 }


})



avg = Object.fromEntries(Object.entries(avg).map(([k,v]) => ([k,v.value/v.count])))



console.log(avg)

let data = [


        {"Age": 26,"Level": 8},


        {"Age": 37,"Level": 9},


        {"Age": null,"Level": 15},


        {"Age": null,"Level": 45}


];



let averages = data.reduce((values, o) => {


        Object.entries(o).forEach(([k, v]) => {


                if (v !== null)


                        values[k] = (values[k] || []).concat(v);


        });


        return values;


}, {});



Object.entries(averages).forEach(([k, vs]) =>


        averages[k] = vs.reduce((a, b) => a + b) / vs.length);



console.log(averages);

(还没有测试)


data = [


 {"Age":26,"Level":8},


 {"Age":37,"Level":9},


 {"Age":null,"Level":15},


 {"Age":null,"Level":45}


];



var sum = {"Age": 0,"Level": 0 };


var average = {"Age": 0,"Level": 0 };


var sumCount = {"Age": 0,"Level": 0 };



// sum up all objects


for (var i = 0; i < data.length; i++) {


 Object.keys(data[i]).forEach(function (key) {


 if (data[i][key] == null || data[i][key] == undefined)


 return;


 sumCount[key]++;


 var Number = data[i][key];


 sum[key] = sum[key] + Number;


 });


}



// make average object


Object.keys(average).forEach(function (key) {


 average[key] = sum[key] / sumCount[key];


});



可以使用简单的for...of循环获取Agesum和非空项的数量。 你可以添加一个get,这样就可以了,


const data = [{Age:26,Level:8},{Age:37,Level:9},{Age:null,Level:15},{Age:null,Level:45}];



let counter = {}



for (const item of data) {


 for (const key in item) {


 if (item[key] !== null) {


 counter[key] = counter[key] || {


 sum: 0,


 counter: 0,


 get avg() { return this.sum/this.counter }


 };


 counter[key].sum += item[key]


 counter[key].counter++


 }


 }


 }



 console.log(counter)
...