我有问题添加一个数组的所有元素以及平均它们。我将如何做到这一点,并实现它与我目前的代码?元素的定义如下所示。

<script type="text/javascript">
//<![CDATA[

var i;
var elmt = new Array();

elmt[0] = "0";
elmt[1] = "1";
elmt[2] = "2";
elmt[3] = "3";
elmt[4] = "4";
elmt[5] = "7";
elmt[6] = "8";
elmt[7] = "9";
elmt[8] = "10";
elmt[9] = "11";

// Problem here
for (i = 9; i < 10; i++){
  document.write("The sum of all the elements is: " + /* Problem here */ + " The average of all the elements is: " + /* Problem here */ + "<br/>");
}   

//]]>
</script>

当前回答

如果你需要平均值并且可以跳过计算和的要求,你可以通过调用reduce来计算平均值:

// Assumes an array with only values that can be parsed to a Float
var reducer = function(cumulativeAverage, currentValue, currentIndex) {
  // 1. multiply average by currentIndex to find cumulative sum of previous elements
  // 2. add currentValue to get cumulative sum, including current element
  // 3. divide by total number of elements, including current element (zero-based index + 1)
  return (cumulativeAverage * currentIndex + parseFloat(currentValue))/(currentIndex + 1)
}
console.log([1, 2, 3, 4, 5, 6, 7, 8, 9, 10].reduce(reducer, 0)); // => 5.5
console.log([].reduce(reducer, 0)); // => 0
console.log([0].reduce(reducer, 0)); // => 0
console.log([].reduce(reducer, 0)); // => 0
console.log([,,,].reduce(reducer, 0)); // => 0
console.log([].reduce(reducer, 0)); // => 0

其他回答

似乎有无数的解决方案,但我发现这个是简洁和优雅的。

const numbers = [1,2,3,4];
const count = numbers.length;
const reducer = (adder, value) => (adder + value);
const average = numbers.map(x => x/count).reduce(reducer);
console.log(average); // 2.5

或者更简洁地说:

const numbers = [1,2,3,4];
const average = numbers.map(x => x/numbers.length).reduce((adder, value) => (adder + value));
console.log(average); // 2.5

根据您的浏览器,您可能需要执行显式函数调用,因为箭头函数不受支持:

const r = function (adder, value) {
        return adder + value;
};
const m = function (x) {
        return x/count;
};
const average = numbers.map(m).reduce(r);
console.log(average); // 2.5

Or:

const average1 = numbers
    .map(function (x) {
        return x/count;
     })
    .reduce(function (adder, value) {
        return adder + value;
});
console.log(average1);

我觉得我们可以

var k=elmt.reduce(function(a,b){return parseFloat(a+parseFloat(b));})
var avg=k/elmt.length; 
console.log(avg);

我使用了两次parseFloat,因为 当 1)你添加(a)9+b(“1”)数字,结果将是“91”,但我们想要加法。所以我使用了parseFloat

2)当添加(a)9+parseFloat(“1”)发生,虽然结果将是“10”,但它将是字符串,我们不想要的,所以我再次使用parseFloat。

我希望我讲清楚了。欢迎提出建议

HTML内容的平均值

使用jQuery或Javascript的querySelector,您可以直接访问格式化的数据…例子:

<p>Elements for an average: <span class="m">2</span>, <span class="m">4</span>,
   <span class="m">2</span>, <span class="m">3</span>.
</p>

因此,使用jQuery

var A = $('.m')
  .map(function(idx) { return  parseInt($(this).html()) })
  .get();
var AVG = A.reduce(function(a,b){return a+b}) / A5.length;

查看其他4种方法(!)来访问itens和平均它:http://jsfiddle.net/4fLWB/

只是为了好玩

让avg =[81、77、-88、195、6.8].reduce ((a, e, i) = >(*我+ e) / (i + 1)); console.log (avg)

在支持es6的浏览器中,这个填充可能会有帮助。

Math.sum = (...a) => Array.prototype.reduce.call(a,(a,b) => a+b)

Math.avg = (...a) => Math.sum(...a)/a.length;

你可以在Math.sum和Math.sum之间共享相同的调用方法。avg和Math。马克斯,如

var maxOne = Math.max(1,2,3,4) // 4;

你可以用数学。总和为

var sumNum = Math.sum(1,2,3,4) // 10

或者如果你有一个数组要求和,你可以使用

var sumNum = Math.sum.apply(null,[1,2,3,4]) // 10

就像

var maxOne = Math.max.apply(null,[1,2,3,4]) // 4