javascript - 遍历嵌套的对象数组,匹配到另一个对象数组

标签 javascript arrays multidimensional-array mapping javascript-objects

我是 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: [ {...}, {...} ] }
 }
 ...
]

我正在尝试通过 displayArr 中的 table_namefield_name 过滤 schemaArr。当匹配时,我想向 displayArr 提供 descriptiondisplay_name。例如:

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。

预先感谢您的帮助!真的很感激。

最佳答案

Object.values()可用于获取 displayArr 对象和 forEach() 的值可用于对其进行迭代。

find()方法可用于在 schemaArr 中查找带有 table_name 的表。如果表存在,则 find()方法可以再次用于查找具有项目的 field_name 的列。

然后 displayArr 对象的定义项可以用这个找到的列值更新。

Object.values(displayArr.sections).forEach(section => {
  section.forEach(item => {
    let table = schemaArr.find(table => table[item.definition.table_name]);

    if (table) {
      // Find column by field_name.
      let obj = table[item.definition.table_name].columns.find(column => column.field === item.definition.field_name);           

      if (obj) {
        // Update definition.
        item.definition.description = obj.description;
        item.definition.display_name = obj.display_name;
      }
    }
  });
});

关于javascript - 遍历嵌套的对象数组,匹配到另一个对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58547881/

相关文章:

Java最小生成树问题

c++ - “检测到堆栈粉碎”错误

java - 多维数组画图

javascript - 无法在使用 ajax 的 div 中加载 HTML

javascript - React 生命周期事件 - 子组件继承旧状态 Prop

javascript - jquery mobile 单页大小上限

c - 如何在 C 中将 union 值写入数组元素?

Javascript动态切换加倍Ajax调用

c++ - 删除存储在数组中的特定类对象

arrays - 为什么二维数组的行为类似于一维指针数组而不是一维整数数组?