I have an object and I need to sum every value
independently with another similar object like in this example :
CharacterStats: { a: 0, b: 2, c: 0, d: 0 }
ItemStats: { a: 0, b: -1, c: 4, d: 0 }
The Result should be
CharacterStats: { a: 0, b: 1, c: 4, d: 0 }
I found this answer How to sum two object values in javascript But I'm using vueJS so my function looks something like this:
export default {
data () {
return {
CharacterStats: { a:0, b:0, c:0, d:0 }
};
},
methods: {
calculStatsItems(ItemsStats) {
var obj = {};
Object.keys(this.CharacterStats ).forEach(function(a){
obj[a] = this.CharacterStats.stat[a] +ItemsStats[a]
})
console.log(obj);
}
},
}
But i keep getting an error telling me "this is undifined" on this line:
Object.keys(this.CharacterStats ).forEach(function(a)
Is there another way to do it or fix it?