TransWikia.com

Object with boolean / “average” of boolean (better explanation)

Stack Overflow Asked by Jean-Philippe M on March 1, 2021

Having this object

        this.form= {
            info: {
                isactive: true
            },
            section1: {
                isComplete: true,
                subs: {
                    person: {
                        isComplete: true
                    },
                    workinfo: {
                        isComplete: true
                    }
                }
            },
            section2: {
                isComplete: false,
                subs: {
                    base: {
                        isComplete: true
                    },
                    address: {
                        isComplete: true
                    },
                    lr: {
                        isComplete: false
                    }
                }
            },
            ...

I would like to be able to set a section’s isComplete property to true/false based on the subs isComplete.

If one of the subs[].isComplete is false then the section.isComplete will be false.

I have investigated the .reduce but without success.

I would like to have it work with any section with many needed subs
I am trying something like below (which does not work) for the moment…

obj = form.section1;
obj.isComplete = false;
for (const key of Object.keys(obj.subs)) {
    obj.isComplete = obj.isComplete && obj.subs[key].isComplete;
}

Any idea?

Thank you

One Answer

does not work

obj = form.section1;
obj.isComplete = false;
for (const key of Object.keys(obj.subs)) {
    obj.isComplete = obj.isComplete && obj.subs[key].isComplete;
}

Obviously this won't work, because false && boolean will always return in false. Please start with true.

obj = form.section1;
obj.isComplete = true;
for (const key of Object.keys(obj.subs)) {
    obj.isComplete = obj.isComplete && obj.subs[key].isComplete;
}

Correct answer by MoxxiManagarm on March 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP