I am currently learning Javascript and facing a challenge involving looping through nested arrays of objects and filtering another array based on specific properties.
Let's take a look at the structure of both arrays:
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: [ {...}, {...} ] }
}
...
]
The goal is to filter schemaArr
based on the table_name
and field_name
specified in the displayArr
. When there is a match, the corresponding description
and display_name
should be added to the displayArr. For instance:
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" //***
}
},
]
}
};
In this scenario, I am focusing only on data from table_1
, but there could be multiple tables referenced within displayArr
.
Given the nesting of these objects, it presents a more complex mapping and filtering task. I am seeking guidance on how to effectively utilize map, filter, and/or forEach for this purpose.
Thank you for your assistance! Your support is greatly appreciated.