【发布时间】:2020-02-21 04:16:41
【问题描述】:
我是 Javascript 新手,正在尝试遍历一个嵌套的对象数组,并根据第一个对象的属性过滤第二个对象数组。
这是两个数组的结构:
const displayArr = {
sections: {
section_1: [
{
style: "single_select_cmp",
definition: {
table_name: "table_1",
field_name: "organization",
}
},
],
section_2: [
{
style: "single_select_cmp",
definition: {
table_name: "table_1",
field_name: "title",
}
},
]
}
};
const schemaArr = [
{
table_1: {
columns: [
{
description: "Tracking Number Desc",
display_name: "Tracking Number",
display_type: "number",
field: "tracking_number",
type: "int"
},
{
description: "Title Desc",
display_name: "Title",
display_type: "multiple lines of text",
field: "title",
type: "text"
},
{
description: "Description Desc",
display_name: "Description",
display_type: "multiple lines of text",
field: "description",
type: "text"
},
{
description: "Organization Desc",
display_name: "Organization",
display_type: "single line of text",
field: "organization",
type: "text"
}
]
}
},
{
table_2: { columns: [ {...}, {...} ] }
},
{
table_3: { columns: [ {...}, {...} ] }
}
...
]
我正在尝试通过table_name 和field_name 在displayArr 中过滤schemaArr。当有匹配时,我想将 description 和 display_name 提供给 displayArr。例如:
const displayArr = {
sections: {
section_1: [
{
style: "single_select_cmp",
definition: {
table_name: "table_1",
field_name: "organization",
description: "Organization Description", //***
display_name: "Organization" //***
}
},
],
section_2: [
{
style: "single_select_cmp",
definition: {
table_name: "table_1",
field_name: "title",
description: "Title Description", //***
display_name: "Title" //***
}
},
]
}
};
在这个例子中,我只是从table_1 中提取,但是displayArr 中可能引用了任意数量的表。
对我来说,鉴于这些对象是嵌套的,这是一个更复杂的映射/过滤情况。我想知道如何正确有效地利用 map、filter 和/或 forEach。
提前感谢您的帮助!真的很感激。
【问题讨论】:
标签: javascript arrays multidimensional-array mapping javascript-objects