I want to create a new object.
An object that adds only values of the same key.
I want to make it in a way that adds num1 to each other and num2 to num2.
The created object has a value of num1 : 4.
But I don't know why the value is NaN
this is my code.
let mydata = [
{num1: 1 , num2: 2 , num3: 3, num4: 4},
{num1: 1 , num2: 2 , num3: 3, num4: 4},
{num1: 1 , num2: 2 , num3: 3, num4: 4},
{num1: 1 , num2: 2 , num3: 3, num4: 4},
]
const sum = {};
for(let prop of mydata){
for(let key in prop){
sum[key] += prop[key];
}
}
console.log(sum);
//{ num1: NaN, num2: NaN, num3: NaN, num4: NaN } => why Nan??