在 JS 中使用值嵌套 Reduce

早上好,在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 record

const assignmentsTotal = [

    {

      name: "assignmentOne",

      totalDifficultyRating: 9,

      totalFunRating: 7

    }

]


//Difficulty and fun rating as separate records

const assignmentsDifficultyTotal = [

    {

      name: "assignmentOne",

      totalDifficultyRating: 9

    }

]

const assignmentsFunTotal = [

    {

      name: "assignmentOne",

      totalFunRating: 7

    }

]

我非常有信心做到这一点的最佳方法是使用reduce方法。经过一番挖掘后,唯一接近我想要实现的目标的是下面的文章,但我无法让它正常工作。有没有一种好的方法可以从上面的起点做到这一点,或者使用array.map创建单独的数组并在使用reduce方法之后会更好吗?


回首忆惘然
浏览 126回答 2
2回答

catspeake

如果您正在数组中寻找相同的“名称”对象,下面应该可以:const reducer = assignments.reduce((total, current) => {     return { name: current.name, difficultyRating : total.difficultyRating + current.difficultyRating, funRating : total.funRating + current.funRating } });如果你想按名称对对象进行分组,请查看 lodash groupby 函数。一般来说,lodash 在所有数组/对象功能中都非常方便。

鸿蒙传说

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);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript