首页 > 解决方案 > 如何将嵌套 Typescript 对象的值映射到 JSON 对象的属性

问题描述

在我正在进行的一个项目中,我们使用名为formly的 Angular 库来动态创建表单。

目前,表单配置被硬编码为一个名为mockForm. 除了属性等于的对象中的属性外,其中的所有属性mockForm都是硬编码的:optionstype'select'

模拟表格

export const mockForm = {
    name: 'root',
    subSections: [
        {
            name: 'Client',
            subSections: [
                {
                    name: 'Contact Information'
                },
                {
                    name: 'Insurance Information'
                }
            ]
        },
        {
            name: 'Sales',
            subSections: [
                {
                    name: 'Overview',
                    subSections: [
                        {
                            name: 'Overview - A',
                            fields: [
                                {
                                    key: 'fieldA1',
                                    type: 'input',
                                    templateOptions: {
                                        label: 'A1',
                                        required: true
                                    }
                                },
                                {
                                    key: 'fieldA2',
                                    type: 'select',
                                    templateOptions: {
                                        label: 'A2',
                                        required: true,
                                        options: []
                                    }
                                }
                            ]
                        },
                        {
                            name: 'Overview - B',
                            fields: [
                                {
                                    key: 'fieldB1',
                                    type: 'input',
                                    templateOptions: {
                                        label: 'B1',
                                        required: false
                                    }
                                },
                                {
                                    key: 'fieldB2',
                                    type: 'select',
                                    templateOptions: {
                                        label: 'B2',
                                        required: false,
                                        options: []
                                    }
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
};

我想options通过使用返回以下对象的 API 来填充属性:

API 返回

{
    "multi_value_fields": {
        "fieldA2": [
            "froodian@outlook.com",
            "gastown@sbcglobal.net",
            "dgriffith@me.com",
            "maradine@live.com",
            "samavati@icloud.com",
            "naupa@comcast.net"
        ],
        "fieldB2": [
            "<3m",
            "<6m",
            "<9m",
            "<12m",
            "+12m",
            "N/A"
        ]
    }
}

从 API 调用返回的对象返回一个 JSON,其属性是其属性equals的key值;这些 JSON 属性的值代表表单的下拉选项。mockFormtype'select'

预期结果应如下所示:

export const mockForm = {
    name: 'root',
    subSections: [
        {
            name: 'Client',
            subSections: [
                {
                    name: 'Contact Information'
                },
                {
                    name: 'Insurance Information'
                }
            ]
        },
        {
            name: 'Sales',
            subSections: [
                {
                    name: 'Overview',
                    subSections: [
                        {
                            name: 'Overview - A',
                            fields: [
                                {
                                    key: 'fieldA1',
                                    type: 'input',
                                    templateOptions: {
                                        label: 'A1',
                                        required: true
                                    }
                                },
                                {
                                    key: 'fieldA2',
                                    type: 'select',
                                    templateOptions: {
                                        label: 'A2',
                                        required: true,
                                        options: [
                                            "froodian@outlook.com",
                                            "gastown@sbcglobal.net",
                                            "dgriffith@me.com",
                                            "maradine@live.com",
                                            "samavati@icloud.com",
                                            "naupa@comcast.net"
                                        ]
                                    }
                                }
                            ]
                        },
                        {
                            name: 'Overview - B',
                            fields: [
                                {
                                    key: 'fieldB1',
                                    type: 'input',
                                    templateOptions: {
                                        label: 'B1',
                                        required: false
                                    }
                                },
                                {
                                    key: 'fieldB2',
                                    type: 'select',
                                    templateOptions: {
                                        label: 'B2',
                                        required: false,
                                        options: [
                                            "<3m",
                                            "<6m",
                                            "<9m",
                                            "<12m",
                                            "+12m",
                                            "N/A"
                                        ]
                                    }
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
};

我没有经历过这样的场景,我不太确定如何解决这个问题(我应该从 JSON 属性开始并映射回mockForm吗?我是否需要手动迭代mockForm才能从 API 调用中填充?)

标签: javascriptjsontypescriptobjectnested-object

解决方案


您的 JSON mockForm非常典型。如果它保持不变,那么您必须手动迭代底部叶子,mokeForm.subSections[1].subSections然后循环到那里以匹配标签类型

否则,您需要编写遍历 mokeForm JSON 的 parse 并分配所需的选项是各自的地方。


推荐阅读