1

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?

4
  • 2
    "I know I can do" That version won't work, o will be an object reference, not a number. How flexible do you need to be? One level of objects? Two? As many as needed? Commented Aug 27, 2020 at 11:18
  • I've already fixed. values instead of entries. finalObj has 4 childs. Each child is like above. Commented Aug 27, 2020 at 11:19
  • 2
    I was talking about the fixed version. finalObj's values are object references. Commented Aug 27, 2020 at 11:19
  • 1
    Ah! Sorry. Just one level. Commented Aug 27, 2020 at 11:29

3 Answers 3

3

You could take a recursive approach for nested objects.

const
    add = object => Object
        .values(object)
        .reduce((s, v) => s + (v && typeof v === 'object' ? add(v) : v), 0),
    one = { '2020-01-01': 4, '2020-01-02': 5 },
    two = { '2020-01-05': 10, '2020-02-10': 15 },
    finalObj = { one, two },
    total = add(finalObj);

console.log(total);

Sign up to request clarification or add additional context in comments.

3 Comments

This will be very useful for me to sum my other nested (3 levels) object however I just cannot add type casting. As I use TypeScript. I have to give as any to all objects: repl.it/repls/AppropriateFrenchPress#index.ts
s is always number.
still have to give v as any i guess. Because TS says Operator '+' cannot be applied to types 'number' and 'unknown'.ts(2365)
2

You can use Object.values and reduce for sum. Works with only one level of objects like in your example.

const obj = {
    "2020-01-01": 4,
    "2020-01-02": 5,
};
const obj2 = {
    "2020-01-05": 10,
    "2020-02-10": 15,
};
const finalObj = { one: obj, two: obj2 };

const result = Object.values(finalObj).reduce((a, b) => a + Object.values(b).reduce((a1, b1) => a1 + b1, 0), 0);

console.log(result);

Comments

2

You've clarified in the comments that you're only dealing with a single layer (as shown in the question). I'd probably use a simple nested loop:

let sum = 0;
for (const obj of Object.values(finalObj)) {  // Loop through the objects
    for (const value of Object.values(obj)) { // Loop through the values
        sum += value;
    }
}

This is very like Nikita's solution, but keeping the loops explicit. :-)

Live Example:

const obj = {
    "2020-01-01": 4,
    "2020-01-02": 5,
};
const obj2 = {
    "2020-01-05": 10,
    "2020-02-10": 15,
};
const finalObj = { one: obj, two: obj2 };

let sum = 0;
for (const obj of Object.values(finalObj)) {
    for (const value of Object.values(obj)) {
        sum += value;
    }
}

console.log(sum);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.