0

How can I convert a string to a Typescript array?. Please help me.

Look at the code:

private validateEmptyOption(): any {
    console.log("CHECKED")
    let isValid = true;
    this.currentForm.sections.forEach((section: Section) => {
        section.fields.forEach((field: Field) => {
            debugger
            if (field.type === 'select') {
                console.log(field)

                const emptyOptionSections = JSON.parse(field.property).emptyOptionSections;

                if ((emptyOptionSections !== undefined) && (emptyOptionSections == null)) {
                    alert('QAZWSX');
                    isValid = false;
                }
             }
        return isValid;
        });
    });
}

This is my Result in console. I want do this convert to array and looping it.

Check This

Expected Result : There should be a validation message to inform that select controller cannot be empty

Actual Result : Form can save without any validation

11
  • What is the string that u want to convert to array? Commented Dec 31, 2019 at 3:06
  • @Plochie I attached it Commented Dec 31, 2019 at 3:22
  • Cant u just parse that string using JSON.parse? Commented Dec 31, 2019 at 3:29
  • Ok how can I do It. Can You help me. @Plochie Commented Dec 31, 2019 at 3:56
  • 1
    Please edit the question to include these changes instead of posting them as comments. Also, how should this 'object' look like? Have a look around and read through the help center. In particular How do I ask a good question? Commented Dec 31, 2019 at 8:48

2 Answers 2

1

If you would use validateEmptyOption(): boolean you would get the hint, that your function does not return any value. The problem is your return statement if inside other functions thus not beeing return by the main one.

There is almost never a need to use any as a type with typescript. (Until you do very complicated things...)

function validateEmptyOption(): boolean {
    console.log("CHECKED")

    return this.currentForm.sections.every((section: Section) => {
        return section.fields.every((field: Field) => {
            debugger
            if (field.type !== 'select') {
                return false;
            }

            console.log(field)

            try {
                const emptyOptionSections = JSON.parse(field.property).emptyOptionSections;
                if ((emptyOptionSections !== undefined) && (emptyOptionSections == null)) {
                    alert('QAZWSX');
                    return false;
                }
            } catch (e) {
                return false;
            }

            return true
        });
    });
}

The every method is used to check if for every value inner function returns true.

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

Comments

0

Because your return statement was written in the wrong place. Check your { } ( )

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.