第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在數(shù)組中求和屬性值的更好方法

在數(shù)組中求和屬性值的更好方法

函數(shù)式編程 2019-07-15 16:45:17
在數(shù)組中求和屬性值的更好方法我有這樣的事情:$scope.traveler = [             {  description: 'Senior', Amount: 50},             {  description: 'Senior', Amount: 50},             {  description: 'Adult', Amount: 75},             {  description: 'Child', Amount: 35},             {  description: 'Infant', Amount: 25 },];現(xiàn)在,為了獲得這個數(shù)組的總數(shù),我執(zhí)行如下操作:$scope.totalAmount = function(){        var total = 0;        for (var i = 0; i < $scope.traveler.length; i++) {               total = total + $scope.traveler[i].Amount;             }        return total;}當(dāng)只是一個數(shù)組時很容易,但是我有其他的數(shù)組,它們具有不同的屬性名,我想要與它們相加。如果我能做這樣的事,我會更高興的:$scope.traveler.Sum({ Amount });但我不知道如何通過這樣一種方式來重復(fù)使用它:$scope.someArray.Sum({ someProperty });回答我決定用@grff-兔子的建議,所以我避免原型本機(jī)對象(陣列)我剛剛對他的答案做了一些修改,驗證了數(shù)組,和的值不為空,這是我的最后實現(xiàn):$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);};
查看完整描述

3 回答

?
catspeake

TA貢獻(xiàn)1111條經(jīng)驗 獲得超0個贊

我知道這個問題有一個被接受的答案,但我想我應(yīng)該加入一個替代方案減少,看到對數(shù)組進(jìn)行求和是減少的典型示例:

$scope.sum = function(items, prop){
    return items.reduce( function(a, b){
        return a + b[prop];
    }, 0);};$scope.travelerTotal = $scope.sum($scope.traveler, 'Amount');

小提琴


查看完整回答
反對 回復(fù) 2019-07-15
?
qq_笑_17

TA貢獻(xiàn)1818條經(jīng)驗 獲得超7個贊

再來一次,這就是nativeJavaScript函數(shù)MapReduce是為(Map和Remote是多種語言的引擎)而構(gòu)建的。

var traveler = [{description: 'Senior', Amount: 50},
                {description: 'Senior', Amount: 50},
                {description: 'Adult', Amount: 75},
                {description: 'Child', Amount: 35},
                {description: 'Infant', Amount: 25}];function amount(item){
  return item.Amount;}function sum(prev, next){
  return prev + next;}traveler.map(amount).reduce(sum);// => 235;
  // or use arrow functionstraveler.map(item => item.Amount).reduce((prev, next) => prev + next);

通過單獨(dú)設(shè)置更小的函數(shù),我們可以再次使用它們。

// Example of reuse.// Get only Amounts greater than 0;// Also, while using Javascript, stick with camelCase.
// If you do decide to go against the standards, // then maintain your decision with all keys as in...
// { description: 'Senior', Amount: 50 }// would be// { Description: 'Senior', Amount: 50 };var travelers = [{description: 'Senior', amount: 50},
                {description: 'Senior', amount: 50},
                {description: 'Adult', amount: 75},
                {description: 'Child', amount: 35},
                {description: 'Infant', amount: 0 }];
                // Directly above Travelers array I changed "Amount" to "amount" to match standards.function amount(item){
  return item.amount;}travelers.filter(amount);// => [{description: 'Senior', amount: 50},
  //     {description: 'Senior', amount: 50},//     {description: 'Adult', amount: 75},
  //     {description: 'Child', amount: 35}];//     Does not include "Infant" as 0 is falsey.


查看完整回答
反對 回復(fù) 2019-07-15
  • 3 回答
  • 0 關(guān)注
  • 546 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號