0

I'm trying to set default value for all my NULL objects. Here's what I have

private setDisplayAmount(summaries: summary[]): void {
    summaries.map(t => {
        // do some magic, and then...
        this.setDefaultValueForEmptyAmounts(t);
    });
}

private setDefaultValueForEmptyAmounts(summary: Summary): void {
    Object.values(summary.displayAmounts).map(property => property || 0);
}

I've no idea why setDefaultValueForEmptyAmounts doesn't work properly...

This will work but is less esthetic:

private setDisplayAmount(summaries: summary[]): void {
    summaries.map(t => {
        // do some magic, and then...
        t.displayAmounts = {
            OneAmount: t.oneAmt || 0,
            TwoAmount: t.twoAmt || 0,
            // ... for all properties
        };

    });
}
3
  • 2
    Object.values(...).map(...) doesn't set values, only results in a array with the values Commented Sep 26, 2018 at 13:20
  • So how can I fix it? Commented Sep 26, 2018 at 13:23
  • You probably want to change your maps to forEaches, and instead of doing Object.values().map(), you want Object.keys(x).forEach(k => x[k] = ...). Or just use for loops, if you don't understand how these functional programming methods work. Commented Sep 26, 2018 at 13:24

1 Answer 1

2

When using map operations, you should always return something. But you could use forEach.

summaries.forEach(summary => {
    Object.keys(summary).forEach(key => {
    summary[key] = summary[key] || 0;
    });
});
Sign up to request clarification or add additional context in comments.

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.