Better way to sum a property value in an array

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

I have something like this:

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


        
16条回答
  •  萌比男神i
    2020-11-22 03:15

    I was already using jquery. But I think its intuitive enough to just have:

    var total_amount = 0; 
    $.each(traveler, function( i, v ) { total_amount += v.Amount ; });
    

    This is basically just a short-hand version of @akhouri's answer.

提交回复
热议问题