I am new to Javascript and am trying to loop through one nested array of objects, and filtering a second array of objects based on properties of the first.
Here are both arrays' structures:
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: [ {...}, {...} ] }
}
...
]
I am trying to filter schemaArr by table_name and field_name in the displayArr. When there is a match, I would like to supply the description and display_name to the displayArr. For example:
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 example, I am only pulling from table_1, however there may be any number of tables referenced in displayArr.
To me, given these objects are nested, this is a more complex mapping/filtering situation. I'm wondering how to correctly and efficiently leverage map, filter, and/or forEach.
Thank you in advance for your help! Really appreciate it.