如何将这些对象转换为打字稿中的数组

时间:2019-12-31 02:50:42

标签: arrays angular typescript

如何将字符串转换为Typescript数组?请帮助我。

查看代码:

    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;
            });
        });
}

这是我在控制台中的结果。我想将其转换为数组并循环。

Check This

预期结果:应该有一条确认消息,告知选择控制器不能为空

实际结果:无需任何验证即可保存表格

2 个答案:

答案 0 :(得分:1)

如果您使用validateEmptyOption(): boolean,则会得到提示,即您的函数未返回任何值。问题是您的return语句是否在其他函数中,从而不被主要函数返回。

几乎没有从不使用 任何 作为带有打字稿的类型。 (直到您做非常复杂的事情为止……)

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
        });
    });
}

every method用于检查内部函数对于每个值是否返回true。

答案 1 :(得分:0)

因为您的return语句写在错误的位置。 检查您的{ } ( )