为了账号安全,请及时绑定邮箱和手机立即绑定

在 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 关注
  • 125 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信