I need help with filtering a list of people's scores based on different time periods. Each person has a 'scoreHistory' array containing their scores at various points in time, and I want to see the total score for each person starting from 0 say 30 days ago.
My current code is partially working, but it seems to be altering the original data set instead of creating a new filtered version. Here is my code snippet:
filteredScores () {
if(!this.people) {
return
}
let allPeople = this.people.slice(0)
let timeWindow = 30 //days
const windowStart = moment().subtract(timeWindow,'days').toDate()
for (const p of allPeople ) {
let filteredScores = inf.scoreHistory.filter(score => moment(score.date.toDate()).isSameOrAfter(windowStart,'day'))
p.scoreHistory=filteredScores
p.score = inf.scoreHistory.reduce(function(sum,item) {
return sum + item.voteScore
},0)
}
return allInf
}
Although the function calculates the total scores correctly, it seems to be modifying the global state instead of creating a new one. Can someone guide me on how to prevent this unintended alteration?
Thank you for any assistance provided.