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

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

試圖將reduce函數(shù)包裝在另一個函數(shù)中以使其更通用

試圖將reduce函數(shù)包裝在另一個函數(shù)中以使其更通用

慕碼人2483693 2022-11-03 10:01:45
我的對象如下所示: electionResults = {  constituency_1:{    party1:'200',    party2:'400'    ...  },  constituency_2:{    party1:'100',    party2:'500'    ...  },  ...}我正在統(tǒng)計這樣的結(jié)果:Object.values(electionResults).reduce((t, {party1}) => t + +party1, 0)我希望能夠像這樣包裝它:function partyTally(myParty){   return Object.values(electionResults).reduce((t, myParty) => t + +party1, 0)}但無法弄清楚如何傳遞myParty給reduce回調(diào)函數(shù)。我自己從 tsv 文件創(chuàng)建了對象,因此可以在需要時輕松重新配置它。提前感謝您的幫助。編輯所需功能應(yīng)為:function partyTally(myParty){   return Object.values(electionResults).reduce((t, myParty) => t + +**myParty**, 0)}
查看完整描述

2 回答

?
哈士奇WWW

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

派對1是問題所在。這里:


function partyTally(myParty){

  return Object.values(electionResults).reduce((t, c) => t + Number(c[myParty]), 0)

}

例如,通過“party1”。


這是一次統(tǒng)計所有選區(qū)的所有政黨的方法:


electionResults = {

  "constituency_1": {

    "party1": "200",

    "party2": "400"

  },

  "constituency_2": {

    "party1": "100",

    "party2": "500"

  }

};


partiesTally = Object.values(electionResults).reduce((result, constituency) => {

  Object.keys(constituency).forEach(party => {

    if(result[party]==undefined) result[party]=0;

    result[party]+=(+constituency[party]);

  });

  return result;

},{});


console.log(partiesTally);


查看完整回答
反對 回復(fù) 2022-11-03
?
Cats萌萌

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

您可以通過使用完整參數(shù)來完成此操作,Object.values而不是嘗試在回調(diào)中立即解構(gòu)。

function partyTally(myParty){
  return Object.values(electionResults).reduce((t, constituencyResults) => t + +constituencyResults[myParty], 0)
  }


查看完整回答
反對 回復(fù) 2022-11-03
  • 2 回答
  • 0 關(guān)注
  • 128 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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