I have this object. Keys are changeable. They are not fixed so I don't want to rely on key.
interface Inf {
[key: string]: number
}
const obj: Inf = {
'2020-01-01': 4,
'2020-01-02': 5,
}
const obj2: Inf = {
'2020-01-05': 10,
'2020-02-10': 15,
}
const finalObj = { one: obj, two: obj2, ... }
What I want to do is sum 10 + 15 + 4 + 5 regardless their key names. I have lodash installed and it has sumBy but it excepts input as array, however, mine is full object.
How can I get 34 in total with this object? Actually my question is half like What is the best way for doing such operation? I know I can do;
let x = 0
Object.values(obj).forEach(o => {
x += o
})
But is there a better way? Maybe shorter, faster?
owill be an object reference, not a number. How flexible do you need to be? One level of objects? Two? As many as needed?valuesinstead ofentries.finalObjhas 4 childs. Each child is like above.finalObj's values are object references.