亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

在 JS 中使用值嵌套 Reduce

在 JS 中使用值嵌套 Reduce

回首憶惘然 2023-10-20 15:12:13
早上好,在array.map之后,我有一個包含相同分配和一些嵌套評級的數組:const assignments = [    {      name: "assignmentOne",      difficultyRating: 1,      funRating: 2    },    {      name: "assignmentOne",      difficultyRating: 3,      funRating: 4    },    {      name: "assignmentOne",      difficultyRating: 5,      funRating: 1    }]現在我想獲得總難度/樂趣評級,它看起來像以下之一://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方法。經過一番挖掘后,唯一接近我想要實現的目標的是下面的文章,但我無法讓它正常工作。有沒有一種好的方法可以從上面的起點做到這一點,或者使用array.map創建單獨的數組并在使用reduce方法之后會更好嗎?
查看完整描述

2 回答

?
catspeake

TA貢獻1111條經驗 獲得超0個贊

如果您正在數組中尋找相同的“名稱”對象,下面應該可以:

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

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


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

TA貢獻1865條經驗 獲得超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);


查看完整回答
反對 回復 2023-10-20
  • 2 回答
  • 0 關注
  • 188 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號