Javascript 对象数组中的求和属性
我对Angular和Javascript相当陌生,因此需要一些指导。我想计算出一个对象数组中值的总和和平均值。通过输入框将对象推入数组,以下是我目前的代码:Javascript 对象数组中的求和属性,javascript,angularjs,arrays,object,Javascript,Angularjs,Arrays,Object,我对Angular和Javascript相当陌生,因此需要一些指导。我想计算出一个对象数组中值的总和和平均值。通过输入框将对象推入数组,以下是我目前的代码: var myApp = angular.module("myApp", []); myApp.controller("myController", function($scope){ $scope.newLog = {}; $scope.logs = [ {project: "", phase: "",
var myApp = angular.module("myApp", []);
myApp.controller("myController", function($scope){
$scope.newLog = {};
$scope.logs = [
{project: "",
phase: "",
date: "",
startTime: "",
intTime: "",
endTime: "",
comments: ""}
];
$scope.saveLog = function(){
//CREATING DELTA TIME
var newTimeLog = $scope.newLog;
var begin = (newTimeLog.startTime).getTime();
var end = (newTimeLog.endTime).getTime();
var i = newTimeLog.intTime;
var ii = parseInt(i);
var intMilisec = ii*60000;
if( isNaN(begin) )
{
return "";
}
if (begin < end) {
var milisecDiff = end - begin;
}else{
var milisecDiff = begin - end;
}
var minusInt = milisecDiff - intMilisec;
var milisec = parseInt((minusInt%1000)/100)
, seconds = parseInt((minusInt/1000)%60)
, minutes = parseInt((minusInt/(1000*60))%60)
, hours = parseInt((minusInt/(1000*60*60))%24);
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
var deltaFormat = hours + " Hours " + minutes + " Minutes";
newTimeLog["deltaTime"] = deltaFormat;
$scope.logs.push($scope.newLog);
$scope.newLog = {};
};
$scope.intSum = function(){
var sum = 0;
for (var i = 0; i < $scope.logs.length; i++){
sum += $scope.logs[i].intTime;
}
return sum;
};
});
$scope.sum = function(items, prop){ //items is the array, prop is the property you want to its value to be calculated
return items.reduce( function(a, b){
return a + parseInt(b[prop]); //in the first attempt, a=0 which is the same as initialValue
}, 0); //initially sum=0, so initialValue is 0
};
var myApp=angular.module(“myApp”,[]);
控制器(“myController”,函数($scope){
$scope.newLog={};
$scope.logs=[
{项目:“”,
阶段:“,
日期:“,
开始时间:“,
intTime:“”,
结束时间:“,
注释:“}”
];
$scope.saveLog=函数(){
//创建增量时间
var newTimeLog=$scope.newLog;
var begin=(newTimeLog.startTime.getTime();
var end=(newTimeLog.endTime).getTime();
var i=newTimeLog.intTime;
var ii=parseInt(i);
var Intmiliesec=ii*60000;
如果(isNaN(开始))
{
返回“”;
}
如果(开始<结束){
var milisecDiff=结束-开始;
}否则{
var milisecDiff=开始-结束;
}
var minusInt=milisecDiff-intMilisec;
var milisec=parseInt((小数%1000)/100)
,秒=parseInt((minusInt/1000)%60)
,分钟=parseInt((minus int/(1000*60))%60)
,hours=parseInt((minusInt/(1000*60*60))%24);
小时数=(小时数<10)?“0”+小时数:小时数;
分钟=(分钟<10)?“0”+分钟:分钟;
秒=(秒<10)?“0”+秒:秒;
var deltaFormat=小时数+小时数+分钟数+分钟数;
newTimeLog[“deltaTime”]=deltaFormat;
$scope.logs.push($scope.newLog);
$scope.newLog={};
};
$scope.intSum=函数(){
var总和=0;
对于(变量i=0;i<$scope.logs.length;i++){
sum+=$scope.logs[i].intTime;
}
回报金额;
};
});
因此,intSum
函数是我遇到问题的地方-我想对所有对象的intTime
属性求和。因此,如果object1的intTime=1
,object2的intTime=2
,object3的intTime=3
则intSum
应该是6
。然而,我目前从IntSum
中得到的是123
$scope.sum = function(items, prop){ //items is the array, prop is the property you want to its value to be calculated
return items.reduce( function(a, b){
return a + parseInt(b[prop]); //in the first attempt, a=0 which is the same as initialValue
}, 0); //initially sum=0, so initialValue is 0
};
任何帮助都将不胜感激 试试看:
sum += parseInt($scope.logs[i].intTime);
$scope.sum = function(items, prop){ //items is the array, prop is the property you want to its value to be calculated
return items.reduce( function(a, b){
return a + parseInt(b[prop]); //in the first attempt, a=0 which is the same as initialValue
}, 0); //initially sum=0, so initialValue is 0
};
而不是:
sum += $scope.logs[i].intTime;
$scope.sum = function(items, prop){ //items is the array, prop is the property you want to its value to be calculated
return items.reduce( function(a, b){
return a + parseInt(b[prop]); //in the first attempt, a=0 which is the same as initialValue
}, 0); //initially sum=0, so initialValue is 0
};
编辑:我建议您看看reduce函数,在本例中,它是在数组上循环的javascript方式:
$scope.sum = function(items, prop){ //items is the array, prop is the property you want to its value to be calculated
return items.reduce( function(a, b){
return a + parseInt(b[prop]); //in the first attempt, a=0 which is the same as initialValue
}, 0); //initially sum=0, so initialValue is 0
};
EDIT2:您将
$scope.logs.intTime
初始化为“
”。第一个值保留在数组中并生成NaN
。
我建议您按如下方式初始化阵列:
$scope.logs = [];
$scope.sum = function(items, prop){ //items is the array, prop is the property you want to its value to be calculated
return items.reduce( function(a, b){
return a + parseInt(b[prop]); //in the first attempt, a=0 which is the same as initialValue
}, 0); //initially sum=0, so initialValue is 0
};
intTime在对象中定义为字符串。因此,当您进行求和时,它实际上是在进行字符串串联。使用pasrseInt转换为数字并求和。当求和+=”时,尝试使用parseInt:
$scope.intSum = function(){
var sum = 0;
for (var i = 0; i < $scope.logs.length; i++){
sum += parseInt($scope.logs[i].intTime);
}
return sum;
};
$scope.sum = function(items, prop){ //items is the array, prop is the property you want to its value to be calculated
return items.reduce( function(a, b){
return a + parseInt(b[prop]); //in the first attempt, a=0 which is the same as initialValue
}, 0); //initially sum=0, so initialValue is 0
};
$scope.intSum=function(){
var总和=0;
对于(变量i=0;i<$scope.logs.length;i++){
sum+=parseInt($scope.logs[i].intTime);
}
回报金额;
};
string
类型的intTime
转换为int
$scope.sum = function(items, prop){ //items is the array, prop is the property you want to its value to be calculated
return items.reduce( function(a, b){
return a + parseInt(b[prop]); //in the first attempt, a=0 which is the same as initialValue
}, 0); //initially sum=0, so initialValue is 0
};
arr.reduce(回调,[initialValue])
callback=function(accumulator,currentValue){return accumulator+currentValue}//accumulator是自上次回调以来的当前和
$scope.sum = function(items, prop){ //items is the array, prop is the property you want to its value to be calculated
return items.reduce( function(a, b){
return a + parseInt(b[prop]); //in the first attempt, a=0 which is the same as initialValue
}, 0); //initially sum=0, so initialValue is 0
};
- 求和函数可以写成:
$scope.sum = function(items, prop){ //items is the array, prop is the property you want to its value to be calculated return items.reduce( function(a, b){ return a + parseInt(b[prop]); //in the first attempt, a=0 which is the same as initialValue }, 0); //initially sum=0, so initialValue is 0 };
- 在您的情况下,我将以
$scope.sum($scope.logs,'iniTime')的形式进行函数调用。
$scope.sum = function(items, prop){ //items is the array, prop is the property you want to its value to be calculated
return items.reduce( function(a, b){
return a + parseInt(b[prop]); //in the first attempt, a=0 which is the same as initialValue
}, 0); //initially sum=0, so initialValue is 0
};
这很有帮助,谢谢。但是它仍然会被卡住,出于某种原因,每次我单击save按钮切换saveLog()时,它都会将intSum()中的sum变量设置为NaN?为了帮助您更多,我需要您修改后的js文件和html来重现您的问题。