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

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

在 JS 中使用值嵌套 Reduce

在 JS 中使用值嵌套 Reduce

回首憶惘然 2023-10-20 15:12:13
早上好,在array.map之后,我有一個包含相同分配和一些嵌套評級的數(shù)組:const assignments = [    {      name: "assignmentOne",      difficultyRating: 1,      funRating: 2    },    {      name: "assignmentOne",      difficultyRating: 3,      funRating: 4    },    {      name: "assignmentOne",      difficultyRating: 5,      funRating: 1    }]現(xiàn)在我想獲得總難度/樂趣評級,它看起來像以下之一://Both the difficulty and fun rating in the same recordconst assignmentsTotal = [    {      name: "assignmentOne",      totalDifficultyRating: 9,      totalFunRating: 7    }]//Difficulty and fun rating as separate recordsconst assignmentsDifficultyTotal = [    {      name: "assignmentOne",      totalDifficultyRating: 9    }]const assignmentsFunTotal = [    {      name: "assignmentOne",      totalFunRating: 7    }]我非常有信心做到這一點的最佳方法是使用reduce方法。經(jīng)過一番挖掘后,唯一接近我想要實現(xiàn)的目標(biāo)的是下面的文章,但我無法讓它正常工作。有沒有一種好的方法可以從上面的起點做到這一點,或者使用array.map創(chuàng)建單獨的數(shù)組并在使用reduce方法之后會更好嗎?
查看完整描述

2 回答

?
catspeake

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

如果您正在數(shù)組中尋找相同的“名稱”對象,下面應(yīng)該可以:

const reducer = assignments.reduce((total, current) => {
    return { name: current.name, difficultyRating : total.difficultyRating + current.difficultyRating, funRating : total.funRating + current.funRating } });

如果你想按名稱對對象進行分組,請查看 lodash groupby 函數(shù)。一般來說,lodash 在所有數(shù)組/對象功能中都非常方便。


查看完整回答
反對 回復(fù) 2023-10-20
?
鴻蒙傳說

TA貢獻1865條經(jīng)驗 獲得超7個贊

const assignments = [{

    name: "assignmentOne",

    difficultyRating: 1,

    funRating: 2

  },

  {

    name: "assignmentOne",

    difficultyRating: 3,

    funRating: 4

  },

  {

    name: "assignmentOne",

    difficultyRating: 5,

    funRating: 1

  },

  {

    name: "assignmentTwo",

    difficultyRating: 5,

    funRating: 3

  },

  {

    name: "assignmentTwo",

    difficultyRating: 5,

    funRating: 1

  }

];


// if you want the totals as an array:

const assignmentsTotalArray = assignments.reduce((totalArr, item) => {

  // check whether the assignment is already in the array

  const assignmentIndex = totalArr.findIndex(elem => elem.name === item.name);


  // if the assignment is not in the array, add it and initialize the totals

  // otherwise update the totals

  if (assignmentIndex === -1) {

    totalArr.push({

      name: item.name,

      totalDifficultyRating: item.difficultyRating,

      totalFunRating: item.funRating

    });

  } else {

    totalArr[assignmentIndex].totalDifficultyRating += item.difficultyRating;

    totalArr[assignmentIndex].totalFunRating += item.funRating;

  }


  return totalArr;

}, []);


console.log('### assignmentsTotalArray:');

console.log(assignmentsTotalArray);


// if you want the totals as an object:

const assignmentsTotalObject = assignments.reduce((totalObj, item) => {

  // if the output object already contains the assignment, sum the ratings

  // otherwise create a new key for the assignment and initialize the ratings

  if (totalObj[item.name]) {

    totalObj[item.name].totalDifficultyRating += item.difficultyRating;

    totalObj[item.name].totalFunRating += item.funRating;

  } else {

    totalObj[item.name] = {

      totalDifficultyRating: item.difficultyRating,

      totalFunRating: item.funRating

    };

  }


  return totalObj;

}, {});


console.log('### assignmentsTotalObject:')

console.log(assignmentsTotalObject);


查看完整回答
反對 回復(fù) 2023-10-20
  • 2 回答
  • 0 關(guān)注
  • 179 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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