更好地计算数组中属性值的方法 [英] Better way to sum a property value in an array

查看:114
本文介绍了更好地计算数组中属性值的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的事情:

  $ scope.traveler = [
{描述:'高级',金额:50},
{描述:'高级',金额:50},
{描述:'成人',金额:75},
{描述:'儿童',金额: 35},
{描述:'婴儿',金额:25},
];

现在有了这个数组的总金额我正在做这样的事情:

  $ scope.totalAmount = function(){
var total = 0;
for(var i = 0; i< $ scope.traveler.length; i ++){
total = total + $ scope.traveler [i] .Amount;
}
总回报;
}

只有一个数组很容易,但我有其他数组不同我希望总结的财产名称。



我会更高兴如果我可以这样做:

  $ scope.traveler.Sum({Amount}); 

但是我不知道如何通过这种方式来重复使用它未来是这样的:

  $ scope.someArray.Sum({someProperty}); 

回答



<我决定使用@gruff-bunny建议,所以我避免原型对象的原型设计(数组)



我只是对他的答案做了一点修改,验证了数组,并且sum的值不为null,这是我的最终实现:

  $ scope.sum = function(items,prop){
if(items = = null){
返回0;
}
返回items.reduce(函数(a,b){
返回b [prop] == null?a:a + b [prop];
},0 );
};


解决方案

因为它是一个数组你可以添加一个函数数组原型。

 旅行者= [
{描述:'高级',金额:50},
{说明:'高级',金额:50},
{说明:'成人',金额:75},
{说明:'儿童',金额:35},
{描述:'婴儿',金额:25},
];

Array.prototype.sum = function(prop){
var total = 0
for(var i = 0,_len = this.length; i< _len; i ++ ){
总计+ =这[i] [prop]
}
返回总额
}

console.log(traveler.sum(金额) ))

小提琴: http://jsfiddle.net/9BAmj/


I have something like this:

$scope.traveler = [
            {  description: 'Senior', Amount: 50},
            {  description: 'Senior', Amount: 50},
            {  description: 'Adult', Amount: 75},
            {  description: 'Child', Amount: 35},
            {  description: 'Infant', Amount: 25 },
];

Now to have a total Amount of this array I'm doing something like this:

$scope.totalAmount = function(){
       var total = 0;
       for (var i = 0; i < $scope.traveler.length; i++) {
              total = total + $scope.traveler[i].Amount;
            }
       return total;
}

It's easy when is only one array, but I have others arrays with a different property name that I would like to sum.

I would be happier If I could do something like this:

$scope.traveler.Sum({ Amount });

But I don't know how to go through this in a way that I could reuse it in the future like this:

$scope.someArray.Sum({ someProperty });

Answer

I decided to use @gruff-bunny suggestion, so I avoid prototyping native object (Array)

I just did a little modification to his answer validating the array and the value to sum aren't null, this is my final implementation:

$scope.sum = function (items, prop) {
    if (items == null) {
        return 0;
    }
    return items.reduce(function (a, b) {
        return b[prop] == null ? a : a + b[prop];
    }, 0);
};

解决方案

Since it is an array you could add a function to the Array prototype.

traveler = [
    {  description: 'Senior', Amount: 50},
    {  description: 'Senior', Amount: 50},
    {  description: 'Adult', Amount: 75},
    {  description: 'Child', Amount: 35},
    {  description: 'Infant', Amount: 25 },
];

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

console.log(traveler.sum("Amount"))

The Fiddle: http://jsfiddle.net/9BAmj/

这篇关于更好地计算数组中属性值的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆