I guess, your goal is to get the total, so I will remove anything you don't need to that goal. Please tell me otherwise in a reply and I will update my answer :)
I will not go into detail how the loop works, because there are very good sites and tutorials for that (best one: MDN). Instead, I will show you step-by-step how I would improve the code.
You can get the total with any of the below code snippets for an undefined number of fields!
const coolObject = {
"one": {
"number1": 1,
"number2": 2
},
"two": {
"number1": 3,
"number2": 4
},
"three": {
"number1": 5,
"number2": 6
}
}
0. (OPTIONAL) You don't have to use strings for field names (safing bytes) and in order to make the code more git-friendly, you should have a dangling comma after every field. Don't forget the semicolon at the end of the variable assignment!
const coolObject = {
one: {
number1: 1,
number2: 2,
},
two: {
number1: 3,
number2: 4,
},
three: {
number1: 5,
number2: 6,
},
};
1. Using a for-loop:
let total = 0;
// iterate over all fields of coolObject
for (numberContainer in coolObject) {
// iterate over all fields of the fields of coolObject
for (number in coolObject[numberContainer]) {
// add the number to the total
total += coolObject[numberContainer][number];
}
}
2. Implement error handling:
let total = 0;
// iterate over all fields of coolObject
for (containerField in coolObject) {
const numberContainer = coolObject[containerField];
// skip field if it is not an object
if (
typeof numberContainer !== 'object' ||
!coolObject.hasOwnProperty(containerField)
) continue;
// iterate over all fields of the fields of coolObject
for (numberField in numberContainer) {
const number = numberContainer[numberField];
// skip field if it is not a number
if (
typeof number !== 'number' ||
!numberContainer.hasOwnProperty(numberField)
) continue;
// add the number to the total
total += number;
}
}
3. Make total const:
const total = (() => {
let tmp = 0;
// iterate over all fields of coolObject
for (containerField in coolObject) {
const numberContainer = coolObject[containerField];
// skip field if it is not an object
if (
typeof numberContainer !== 'object' ||
!coolObject.hasOwnProperty(containerField)
) continue;
// iterate over all fields of the fields of coolObject
for (numberField in numberContainer) {
const number = numberContainer[numberField];
// skip field if it is not a number
if (
typeof number !== 'number' ||
!numberContainer.hasOwnProperty(numberField)
) continue;
// add the number to the total
tmp += number;
}
}
return tmp;
})();
4. (OPTIONAL) We could optimize the whole procedure and make it more functional, if we are able to change the coolObject to use Arrays instead of fields and Objects:
const coolArray = [
[1,2],
[3,4],
[5,6],
];
That would allow us to use convenience methods on the arrays:
const total = coolArray.reduce(
(acc, arr) => acc + arr.reduce(
(acc, number) => acc + number,
0
),
0
);
5. (OPTIONAL) Above form can be enhanced again with error management:
const total = coolArray.reduce(
(acc, arr) => !Array.isArray(arr)
? acc
: acc + arr.reduce(
(acc, number) => typeof number !== 'number'
? acc
: acc + number,
0
),
0
);