简体   繁体   English

JavaScript中数组内的平均值

[英]JavaScript average of values inside an array

The JSON: 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"}]

The JavaScript: 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);
});

What I'm trying to do is make an average of each value from the 3 answer s, which in case will give these 8 averages: 我要做的是从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])); 

You may use: 你可以使用:


Working Example: 工作实例:

 let data = [{"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"}]; let result = data.map(o => { /* Split the string into array using , delimiter */ let a = o["answer"].split(","); /* * Return average of array by first creating sum using .reduce() and * then dividing by number of array elements. */ return a.reduce((a,c) => (Number(c) + a), 0) / a.length; }); console.log(result); 

create a new array from the answer then sum all the values and divide by its size answer创建一个新数组,然后将所有值相加并除以其大小

 var res = [{ "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" } ]; res.forEach(function(item) { //creating array from the string var imtArray = item.answer.split(','); //summing the element var avg = imtArray.reduce(function(p, q) { // converting string to number return +p + +q }) console.log(avg / imtArray.length) }) 

You're getting the answer , but now you need to split it by commas and parse it to a number. 你得到了answer ,但现在你需要用逗号分割它并将其解析为一个数字。 This gives you an array for each answer , now you can simply iterate through each one and add the numbers then divide them by their count (ie array length) to calculate the average. 这为每个answer提供了一个数组,现在您可以简单地遍历每个answer并添加数字然后除以它们的计数(即数组长度)来计算平均值。

Note: the values in the answers are strings, so make sure you convert them to numbers before you add them together, otherwise you will be adding them together as characters. 注意:答案中的值是字符串,因此请确保在将它们添加到一起之前将它们转换为数字,否则您将它们作为字符一起添加。

 var data = [{"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"}] var arr = []; for (var i = 0; i < data.length; i++) { var d = data[i].answer.split(","); var sum = 0; for (var j = 0; j < d.length; j++) { sum += parseInt(d[j]); } arr.push(sum / d.length); } console.log(arr); 

It seems from your comments that instead of the average of values in each of the three answer s, you actually want the average of each value from the three answer s. 这似乎从你的意见是,除了在三个的平均值的answer S,你真正想要的每个值从三个平均answer秒。 Similar to the code above, you just need to make a little change. 与上面的代码类似,您只需要做一点改动。 You'll have to iterate and add the values of each answer first, then iterate again and calculate the averages: 您必须先迭代并添加每个answer的值,然后再次迭代并计算平均值:

 var data = [{"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"}] var arr = []; for (var i = 0; i < data.length; i++) { var d = data[i].answer.split(","); for (var j = 0; j < d.length; j++) { d[j] = parseInt(d[j]); } if (arr.length == 0) arr = d; else { for (var j = 0; j < d.length; j++) { arr[j] += d[j]; } } } for (var i = 0; i < arr.length; i++) { arr[i] = arr[i] / data.length; } console.log(arr); 

Note: This assumes that each answer has the exact same number of values. 注意:这假定每个answer具有完全相同的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM