jon*_*ler 152 javascript arrays average
我在添加数组的所有元素以及平均它们时遇到了问题.我该怎么做并用我目前的代码实现它?应该定义元素,如下所示.
<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>
Run Code Online (Sandbox Code Playgroud)
Ser*_*lla 447
我认为更优雅的解决方案:
var sum, avg = 0;
// dividing by 0 will return Infinity
// arr must contain at least 1 element to use reduce
if (arr.length)
{
sum = arr.reduce(function(a, b) { return a + b; });
avg = sum / arr.length;
}
document.write("The sum is: " + sum + ". The average is: " + avg + "<br/>");
Run Code Online (Sandbox Code Playgroud)
Mar*_*cck 122
var sum = 0;
for( var i = 0; i < elmt.length; i++ ){
sum += parseInt( elmt[i], 10 ); //don't forget to add the base
}
var avg = sum/elmt.length;
document.write( "The sum of all the elements is: " + sum + " The average is: " + avg );
Run Code Online (Sandbox Code Playgroud)
只需遍历数组,因为您的值是字符串,所以必须先将它们转换为整数.而平均值只是值的总和除以值的数量.
Abd*_*UMI 96
const average = arr => arr.reduce( ( p, c ) => p + c, 0 ) / arr.length;
const result = average( [ 4, 4, 5, 6, 6 ] ); // 5
console.log(result);Run Code Online (Sandbox Code Playgroud)
Tom*_*zyk 30
使用reduce和ES6 计算平均值(平均值):
const average = list => list.reduce((prev, curr) => prev + curr) / list.length;
const list = [0, 10, 20, 30]
average(list) // 15
Run Code Online (Sandbox Code Playgroud)
Shi*_*kin 20
通常使用单线减少的平均值是这样的
elements.reduce(function(sum, a,i,ar) { sum += a; return i==ar.length-1?(ar.length==0?0:sum/ar.length):sum},0);
Run Code Online (Sandbox Code Playgroud)
专门问问题
elements.reduce(function(sum, a,i,ar) { sum += parseFloat(a); return i==ar.length-1?(ar.length==0?0:sum/ar.length):sum},0);
Run Code Online (Sandbox Code Playgroud)
一个有效的版本就像
elements.reduce(function(sum, a) { return sum + a },0)/(elements.length||1);
Run Code Online (Sandbox Code Playgroud)
在1分钟内了解Javascript数组减少 http://www.airpair.com/javascript/javascript-array-reduce
正如gotofritz所指出的那样,Array.reduce会跳过未定义的值.所以这是一个修复:
(function average(arr){var finalstate=arr.reduce(function(state,a) { state.sum+=a;state.count+=1; return state },{sum:0,count:0}); return finalstate.sum/finalstate.count})([2,,,6])
Run Code Online (Sandbox Code Playgroud)
chi*_*ens 17
平均最短的一个班轮:
let avg = [1,2,3].reduce((a,v,i)=>(a*i+v)/(i+1));
Run Code Online (Sandbox Code Playgroud)
总和最短的一个班轮:
let sum = [1,2,3].reduce((a,b)=>a+b);
Run Code Online (Sandbox Code Playgroud)
小智 14
让我们假设我们有一个这样的整数数组:
var values = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
Run Code Online (Sandbox Code Playgroud)
平均值通过以下公式获得
A =(1/n)Σxi (i = 1到n) ......所以:x1/n + x2/n + ... + xn/n
我们将当前值除以值的数量,并将先前的结果添加到返回的值.
reduce方法签名是
reduce(callback[,default_previous_value])
Run Code Online (Sandbox Code Playgroud)
reduce回调函数采用以下参数:
第二个reduce参数是默认值 ...(用于数组为空的情况).
所以平均reduce方法将是:
var avg = values.reduce(function(p,c,i,a){return p + (c/a.length)},0);
Run Code Online (Sandbox Code Playgroud)
如果您愿意,可以创建单独的功能
function average(p,c,i,a){return p + (c/a.length)};
function sum(p,c){return p + c)};
Run Code Online (Sandbox Code Playgroud)
然后简单地参考回调方法签名
var avg = values.reduce(average,0);
var sum= values.reduce(sum,0);
Run Code Online (Sandbox Code Playgroud)
或者直接扩充Array原型..
Array.prototype.sum = Array.prototype.sum || function (){
return this.reduce(function(p,c){return p+c},0);
};
Run Code Online (Sandbox Code Playgroud)
每次调用reduce方法时都可以对值进行除法.
Array.prototype.avg = Array.prototype.avg || function () {
return this.reduce(function(p,c,i,a){return p+(c/a.length)},0);
};
Run Code Online (Sandbox Code Playgroud)
或者甚至更好,使用预先定义的 Array.protoype.sum()
方法,优化过程我只调用一次除法:)
Array.prototype.avg = Array.prototype.avg || function () {
return this.sum()/this.length;
};
Run Code Online (Sandbox Code Playgroud)
然后在范围的任何Array对象上:
[2, 6].avg();// -> 4
[2, 6].sum();// -> 8
Run Code Online (Sandbox Code Playgroud)
注意:在我的观点中,返回NaN愿望的空数组比0更正确,并且在特定用例中非常有用.
Gen*_*wen 13
您还可以在数学部分中使用lodash,_.sum(数组)和_.mean(数组)(还有其他方便的工作人员).
_.sum([4, 2, 8, 6]);
// => 20
_.mean([4, 2, 8, 6]);
// => 5
Run Code Online (Sandbox Code Playgroud)
不是最快的,但是最短且在一行中使用map()和reduce():
var average = [7,14,21].map(function(x,i,arr){return x/arr.length}).reduce(function(a,b){return a + b})
Run Code Online (Sandbox Code Playgroud)
我在个人库中使用以下方法:
Array.prototype.sum = Array.prototype.sum || function() {
return this.reduce(function(sum, a) { return sum + Number(a) }, 0);
}
Array.prototype.average = Array.prototype.average || function() {
return this.sum() / (this.length || 1);
}
Run Code Online (Sandbox Code Playgroud)
编辑:要使用它们,只需向数组求和或平均值即可,例如:
[1,2,3].sum() // = 6
[1,2,3].average() // = 2
Run Code Online (Sandbox Code Playgroud)
In ES6-ready browsers this polyfill may be helpful.
Math.sum = (...a) => Array.prototype.reduce.call(a,(a,b) => a+b)
Math.avg = (...a) => this.sum(...a)/a.length;
Run Code Online (Sandbox Code Playgroud)
You can share same call method between Math.sum,Math.avg and Math.max,such as
var maxOne = Math.max(1,2,3,4) // 4;
Run Code Online (Sandbox Code Playgroud)
you can use Math.sum as
var sumNum = Math.sum(1,2,3,4) // 10
Run Code Online (Sandbox Code Playgroud)
or if you have an array to sum up,you can use
var sumNum = Math.sum.apply(null,[1,2,3,4]) // 10
Run Code Online (Sandbox Code Playgroud)
just like
var maxOne = Math.max.apply(null,[1,2,3,4]) // 4
Run Code Online (Sandbox Code Playgroud)