I have two objects and I want to add the values of each key separately with another object. Here's an example:
CharacterStats: { a: 0, b: 2, c: 0, d: 0 }
ItemStats: { a: 0, b: -1, c: 4, d: 0 }
The expected result is:
CharacterStats: { a: 0, b: 1, c: 4, d: 0 }
I came across this solution on How to sum two object values in JavaScript. However, as I'm working with vueJS, my function looks something like this:
export default {
data () {
return {
CharacterStats: { a:0, b:0, c:0, d:0 }
};
},
methods: {
calculStatsItems(ItemStats) {
var obj = {};
let self = this;
Object.keys(self.CharacterStats).forEach(function(a){
obj[a] = self.CharacterStats[a] + ItemStats[a];
});
console.log(obj);
}
}
}
However, I keep encountering an error that says "this is undefined" on this line:
Object.keys(this.CharacterStats ).forEach(function(a)
Is there any other way to solve this issue or fix it?