早上好,在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方法之后会更好吗?
catspeake
鸿蒙传说
相关分类