gpt4 book ai didi

数组内值的 JavaScript 平均值

转载 作者:行者123 更新时间:2023-11-30 11:23:19 28 4
gpt4 key购买 nike

JSON:

[{"id":"1","user":"001","answer":"1,1,3,2,2,1,3,2"},
{"id":"2","user":"002","answer":"2,3,3,2,1,1,3,2"},
{"id":"3","user":"003","answer":"3,5,3,5,3,1,3,2"}]

JavaScript:

jQuery.ajax({
url: "file.json",
type: "POST",
dataType: "json",
async: false,
success: function (data) {
var arr = [];
for (i = 0; i < data.length; i++) {
arr.push(data[i].answer);
}
console.log(arr);
});

我想做的是对 3 个 answer 的每个值取平均值,以防万一,这将给出这 8 个平均值:

2,3,3,3,2,1,3,2

最佳答案

const arrAvg = arr => arr.reduce((a,b) => a + b, 0) / arr.length;
console.log(arrAvg([1,2,3,4,5]));

关于数组内值的 JavaScript 平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48974505/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com