AngularJS在对象

时间:2015-12-11 08:07:54

标签: javascript reduce

我是AngularJS的新手,并且一直在构建以下内容:

app.js

$scope.inkomsten= [
    { name: 'Salaris',value:'500' },
    { name: 'Toeslag',value:'200' },
    { name: 'Inkomsten',value:'211' },
    { name: 'Weekend', value:'22' }
];

$scope.uitgiften= [
    {   name: 'eten',value:'120'},
    { name: 'kat',value:'230'},
    { name: 'schildpad', value: '300'},
    { name: 'huur', value: '200'}
];

现在我想获取这两个对象的值并将它们相互减去! 因此,要么将Inkomsten的所有值添加到另一个Uitgiften中,要么将uitgiften添加到...中然后减去或单独减少(这是首选...所以取inkomsten的值然后来自uitgiften.value[1]+uitgiften.value[2]+uitgiften.value[3] and then inkomsten.value[1]+inkomsten.value[2]+inkomsten.value[3] 并减去它们。)

我一直在修补它,但我似乎无法找到解决方案。无论如何,不​​是在Angular:D

我希望你们能帮忙

澄清:

对不起。我想要

outcomeInkomsten-outcomeUitgiften=endOutcome

然后这两个总和的结果必须是*.log

5 个答案:

答案 0 :(得分:0)

由于您的值是字符串,因此您需要将它们转换为数字。这就是我要做的事情:

var result = 0;
$scope.inkomsten.forEach(function(item){
  result += Number(item.value);
});
$scope.uitgiften.forEach(function(item){
  result -= Number(item.value);
});

答案 1 :(得分:0)

你可以使用array方法reduce:

var $scope={};
$scope.inkomsten= [
    { name: 'Salaris',value:'500' },
    { name: 'Toeslag',value:'200' },
    { name: 'Inkomsten',value:'211' },
    { name: 'Weekend', value:'22' }
];

var res = $scope.inkomsten.reduce(function(total, current){
    return total + Number(current.value);
}, 0)

console.log(res)

这将为您提供数组的数字;

然后你可以对第二个数组做同样的事情,然后减去两个数组;

答案 2 :(得分:0)

你可以使用

var results=0;
for(i=0;i<$scope.inkomsten.length;i++){
      results+=Number($scope.inkomsten[i].value);
}
for(i=0;i<$scope.uitgiften.length;i++){
      results-=Number($scope.uitgiften[i].value);
}
console.log(results);

答案 3 :(得分:0)

由于它是一个数组,你可以向Array原型添加一个函数。

$scope.inkomsten= [
   { name: 'Salaris',value:'500' },
   { name: 'Toeslag',value:'200' },
   { name: 'Inkomsten',value:'211' },
   { name: 'Weekend', value:'22' }
];

$scope.uitgiften= [
   { name: 'eten',value:'120'},
   { name: 'kat',value:'230'},
   { name: 'schildpad', value: '300'},
   { name: 'huur', value: '200'}
];

Array.prototype.sum = function (prop) {
    var total = 0;
    for ( var i = 0, _len = this.length; i < _len; i++ ) {
        total += parseInt(this[i][prop]);
    }
    return total;
}

$scope.endOutcome = $scope.inkomsten.sum("value") - $scope.uitgiften.sum("value");

小提琴: http://jsfiddle.net/U3pVM/20778/

答案 4 :(得分:0)

您可以尝试以下代码

$scope.inkomsten= [
       { name: 'Salaris',value:'500' },
       { name: 'Toeslag',value:'200' },
       { name: 'Inkomsten',value:'211' },
       { name: 'Weekend', value:'22' }
    ];
$scope.uitgiften= [
       { name: 'eten',value:'120'},
       { name: 'kat',value:'230'},
       { name: 'schildpad', value: '300'},
       { name: 'huur', value: '200'}
    ];

$scope.sum1 = 0;
$scope.sum2 = 0;

for(i in $scope.inkomsten){
    $scope.sum1 = $scope.sum1 + parseInt($scope.inkomsten[i].value);
}

for(i in $scope.uitgiften){
    $scope.sum2 = $scope.sum2 + parseInt($scope.uitgiften[i].value);
}

$scope.endOutcome = $scope.sum1 - $scope.sum2;

alert($scope.endOutcome);