【问题标题】:Looping through nested array of objects, matching to another array of objects遍历嵌套的对象数组,匹配另一个对象数组
【发布时间】: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_namefield_namedisplayArr 中过滤schemaArr。当有匹配时,我想将 descriptiondisplay_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


    【解决方案1】:

    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;
          }
        }
      });
    });
    

    【讨论】:

    • 这很有意义。我意识到我说 schemaArr 是一个对象,但实际上它是一个表对象数组。我已经相应地更新了我的问题。
    • @BWeb303 - 没问题,我们可以使用find() 方法在数组中查找表。但是您编辑的问题中的schemaArr 无效,数组不能有属性名称。你有类似schemaArr = [{table_1: ...}, {table_2: ...}, ...] 的东西吗?
    • 啊……你说得对。我很抱歉。客户的数据结构如下:schemaArr = [ {table_1: { columns: [ {...}, {...} ] } }, {table_2:....} ]。当然不是最佳实践,但这就是我正在使用的。我已经更新了我的问题以反映真实的架构结构。这可能会改变您在回复中设置 table 的方式。
    • @BWeb303 - 没问题。我是这么想的。请参阅我编辑的答案。另外,displayArr 是对象还是数组?如果您有任何问题,请告诉我。
    • 实际上我只是按原样调用sections。我不会将sections 填充到displayArr 或其他对象中。所以我想我会改用Object.values(sections)。真的很抱歉我的所有错误。感谢您的帮助。
    猜你喜欢
    • 2020-07-25
    • 2022-01-14
    • 1970-01-01
    • 2020-06-22
    • 2018-08-28
    • 1970-01-01
    • 2021-12-28
    • 2020-04-13
    • 1970-01-01
    相关资源
    最近更新 更多