Better way to sum a property value in an array

后端 未结 16 1486
遥遥无期
遥遥无期 2020-11-22 02:41

I have something like this:

$scope.traveler = [
            {  description: \'Senior\', Amount: 50},
            {  description: \'Senior\', Amount: 50},
             


        
16条回答
  •  后悔当初
    2020-11-22 03:12

    I'm not sure this has been mentioned yet. But there is a lodash function for that. Snippet below where value is your attribute to sum is 'value'.

    _.sumBy(objects, 'value');
    _.sumBy(objects, function(o) { return o.value; });
    

    Both will work.

提交回复
热议问题