What is the best way to subtract or add values from identical objects?
const obj1 = {
1: 10,
2: 10,
3: 10,
4: 10,
};
const obj2 = {
1: 30,
2: 30,
3: 30,
4: 30,
};
const result = obj2 - obj1;
console.log(result);
/* the expected output is:
{
1:20,
2: 20,
3: 20,
4: 20,
};
*/
ESLint points out that using for in to add values is not a good practice and the log is "guard-for-in: The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype. "
for (const p in obj2) {
newObj[p] = obj2[p] - obj1[p];
}
console.log(newObj);
Doing with for in I also had problems returning unwanted values like $init":0
when using an array of objects.