Javascript 如何将嵌套Typescript对象的值映射到JSON对象的属性
在我正在进行的一个项目中,我们使用名为Angular的库来动态创建表单 目前,表单配置被硬编码为名为Javascript 如何将嵌套Typescript对象的值映射到JSON对象的属性,javascript,json,typescript,object,nested-object,Javascript,Json,Typescript,Object,Nested Object,在我正在进行的一个项目中,我们使用名为Angular的库来动态创建表单 目前,表单配置被硬编码为名为mockForm的Typescript对象。mockForm中的所有属性都是硬编码的,除了选项属性之外,对象的类型属性等于选择: 模拟表单 export const mockForm = { name: 'root', subSections: [ { name: 'Client', subSections: [
mockForm
的Typescript对象。mockForm
中的所有属性都是硬编码的,除了选项
属性之外,对象的类型
属性等于选择
:
模拟表单
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: []
}
}
]
}
]
}
]
}
]
};
我想通过使用一个名为的API来填充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"
]
}
}
我还没有经历过这样的场景,我也不太确定如何处理(我应该从JSON属性开始并映射回
mockForm
?我是否需要手动迭代mockForm
,以便从API调用填充?您的JSONmockForm非常典型。
如果保持不变,则必须手动迭代底部叶,即,mokeForm.subSections[1]。subSections
,然后在那里循环以匹配标签&类型
否则,您需要编写遍历mokeForm JSON的parse,并将所需的选项分配到各自的位置。您可能正在使用TypeScript,但这似乎不是特定于TypeScript的。在我看来,这更像是一个JavaScript问题。也许你应该添加JavaScript标签?
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"
]
}
}
]
}
]
}
]
}
]
};