将多个数组的每个项目的平均值放到一个数组中

时间:2017-08-30 21:32:26

标签: javascript jquery html arrays underscore.js

我的大脑正在融化......我正在努力完成以下任务:

我知道每个数组有多少个数组和多少个元素。 这些数字是动态的,但我们可以说:3个数组,每个数组包含18个元素。

示例:

["106","142","112","77","115","127","87","127","156","118","91","93","107","151","110","79","40","186"]

["117","139","127","108","172","113","79","128","121","104","105","117","139","109","137","109","82","137"]

["111","85","110","112","108","109","107","89","104","108","123","93","125","174","129","113","162","159"]

现在我想得到所有三个数组中元素1的平均值,以及所有三个数组中的元素2,依此类推。

最终结果应该是一个数组,其中包含所有18个元素的平均值。

类似的东西:

var result_array = [];
for (i = 0; i < 3; i++) {  
  result_array.push(arrayone[i] + arraytwo[i] + arraythree[i]) / 3
}

如果3被修复,这将有效,但数组的数量是动态的。

希望这有意义......

6 个答案:

答案 0 :(得分:2)

&#13;
&#13;
FROM debian:9
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您确实使用underscore.js标记了此问题,因此您可以使用_.zip方法。这将所有第一个元素放在一个数组中,依此类推。然后,您可以对每个阵列进行平均。

请参阅CodePen

git fetch

使用ES6箭头功能(CodePen):

var arr1 = ["106","142","112","77","115","127","87","127","156","118","91","93","107","151","110","79","40","186"]
var arr2 = ["117","139","127","108","172","113","79","128","121","104","105","117","139","109","137","109","82","137"]
var arr3 = ["111","85","110","112","108","109","107","89","104","108","123","93","125","174","129","113","162","159"]
// ... as many more arrays as you want

var avgEmAll = function (arrays) {
  // zip with array of arrays https://stackoverflow.com/a/10394791/327074
  return _.zip.apply(null, arrays).map(avg)
}

// average an array https://stackoverflow.com/a/10624256/327074
var avg = function (x) {
  return x.reduce(function (y, z) {return Number(y) + Number(z)}) / x.length
}
console.log(avgEmAll([arr1, arr2, arr3]))

答案 2 :(得分:0)

这里的答案是使用循环。我们将您的数组称为arr1,arr2和arr3。

var averages = [];

for(i = 0; i < arr1.length; i++) {
    var a = arr1[i];
    var b = arr2[i];
    var c = arr3[i];
    var avg = (a + b + c) / 3;
    averages.push(avg);
}

对于此循环的每次迭代,它将:
- 将每个数组的下一个数字赋给变量(从索引0开始)
- 找出三个数字的平均值 - 将计算结果添加到averages数组

答案 3 :(得分:0)

用Java编写了这个解决方案但你可以获得逻辑方面的帮助。希望它有所帮助。

ArrayList<Integer> averageArrayList = new ArrayList<>();
int arrayListLength = arrayList1.length(); //assuming previous arraylists have same size.


   for(int i=0; i<arrayListLength; i++){
        int averageValue = (arrayList1.get(i) + arrayList2.get(i) + arrayList3.get(i)) / 3;
        //adds average value to current index.
        averageArrayList.add(i, averageValue);
    }
//averageArrayList is ready with your values..

答案 4 :(得分:0)

此函数将采用任意数量的数组并计算其平均值。它使用+运算符自动将字符串转换为数字。它是动态的,因为它不关心数组的长度或数量,只要它们的长度都相等。由于我们可以假设它们的长度都相同(基于问题),因此它会迭代第一个数组的长度。它没有圆,因为没有要求。

function average_of_arrays(...arrays) {
  let result = [];
  for(let array_index in arrays[0]) {
    let total = 0;
    for (let arr of arrays) {
      total += +arr[array_index]
    }
    result.push(total / arrays.length);
  }
  return result;
}

let arr1 = ["106", "142", "112", "77", "115", "127", "87", "127", "156", "118", "91", "93", "107", "151", "110", "79", "40", "186"];

let arr2 = ["117", "139", "127", "108", "172", "113", "79", "128", "121", "104", "105", "117", "139", "109", "137", "109", "82", "137"];

let arr3 = ["111", "85", "110", "112", "108", "109", "107", "89", "104", "108", "123", "93", "125", "174", "129", "113", "162", "159"];

function average_of_arrays(...arrays) {
  let result = [];
  for(let array_index in arrays[0]) {
    let total = 0;
    for (let arr of arrays) {
      total += +arr[array_index]
    }
    result.push(total / arrays.length);
  }
  return result;
}


console.log(average_of_arrays(arr1, arr2, arr3));

答案 5 :(得分:0)

var result = getAvg( [ 1, 2, 3 ], [ 2, 3, 4 ] )

console.log( result ) // [ 1.5, 2.5, 3.5 ]

function getAvg() {

  var a = arguments
  var nar = a.length
  var nel = a[ 0 ].length
  var el, ar, avg, avgs = []

  for( el = 0; el < nel; ++el ) {
    avg = 0
    for( ar = 0; ar < nar; ++ar ) avg += a[ ar ][ el ]
    avgs[ el ] = avg / nar
  }

  return avgs

}