Hey there! I hope your day is going well. I'm currently working on calculating multiple objects to create a new one with average values.
Here's the schema:
export class stats{
assists:number
causedEarlySurrender:boolean
champLevel:number
...
}
Then, I have this data in an array of objects like below:
0: {assists: 0, causedEarlySurrender: false, champLevel: 18, ...}
1: {assists: 2, causedEarlySurrender: false, champLevel: 18, ...}
2: {assists: 0, causedEarlySurrender: false, champLevel: 16, ...}
I've been experimenting but struggling to find a way to automate this process without having to manually type each key name.
This is what I have tried so far:
meanStats(){
let iterator = 0;
this.stats.forEach((m, i, array)=>{
iterator ++;
for (let [key, value] of Object.entries(m)) {
console.log(key, value);
switch (value) {
case Boolean:
if(key == "win"){
this.wins += value//if true should compute as 1 and if its not should be 0
}
if(key == "gameEndedInSurrender"){
this.surrenders += value
}
else{
break
}
break;
case Number:
this.meanStat.key += value
break;
default:
break;
}
}
});
}
The goal is to iterate through each key name and value, determine if it's a boolean or a number, sum that value on the correct key of the new object, and then divide all values by the iterator to obtain an average. The current issue is that 'key' is not recognized as a valid value of the object.
Any assistance on this matter would be greatly appreciated. Thank you in advance!