代码之家  ›  专栏  ›  技术社区  ›  mr__brainwash

收集深度嵌套数据的平面数组

  •  0
  • mr__brainwash  · 技术社区  · 6 年前

    var objectSchemasList = {
      1: [
        {
            name: 'list_field1_1',
          uuid: 'uuid1',
          fieldObjectSchemaId: 2
        },
        {
            name: 'list_field1_2',
          uuid: 'uuid2',
          fieldObjectSchemaId: null
        },
      ],
      2: [
        {
            name: 'list_field2_1',
          uuid: 'uuid3',
          fieldObjectSchemaId: null
        },
        {
            name: 'list_field2_2',
          uuid: 'uuid4',
          fieldObjectSchemaId: null
        },
      ],
      3: [
        {
            name: 'list_field3_1',
          uuid: 'uuid5',
          fieldObjectSchemaId: 1
        },
        {
            name: 'list_field3_2',
          uuid: 'uuid6',
          fieldObjectSchemaId: null
        },
      ],
    }
    

    以及与之相关的数据数组:

    const objectSchemaFields = [
        {
        name: 'field_1',
        uuid: '_uuid1',
        fieldObjectSchemaId: null
      },
      {
        name: 'field_2',
        uuid: '_uuid2',
        fieldObjectSchemaId: null
      },
      {
        name: 'field_3',
        uuid: '_uuid3',
        fieldObjectSchemaId: 1
      },
    ];
    

    这意味着每个对象模式字段都可以在自身内部包含其他字段。由 fieldObjectSchemaId . 这意味着 objectSchemaFields[2] 元素使用 objectSchemasList[objectSchemaFields[2].fieldObjectSchemaId] . 也会用到 objectSchemasList[2] tried . 最终数组应该是平的,并且只有 path, name, uuid 财产。其中路径由父名称和按点拆分的所有嵌套子名称的连接组成。例如,结果应该是:

    const result = [
      {
        path: 'field_1',
        name: 'field_1',
        uuid: '_uuid1',
        },
      {
        path: 'field_2',
            name: 'field_2',
        uuid: '_uuid2',
        },
      {
        path: 'field_3',
        name: 'field_3',
        uuid: '_uuid3',
      },
      {
        path: 'field_3.list_field1_1',
        name: 'list_field1_1',
        uuid: 'uuid1',
      },
      {
        path: 'field_3.list_field1_1.list_field2_1',
        name: 'list_field2_1',
        uuid: 'uuid3',
      },
      {
        path: 'field_3.list_field1_1.list_field2_2',
        name: 'list_field2_2',
        uuid: 'uuid4',
      },
      {
        path: 'field_3.list_field1_2',
        name: 'list_field1_2',
        uuid: 'uuid2',
        }
    ]
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   riv    6 年前

    这不是一个很好的map用例,因为您仍然需要返回原始对象和子对象,并且需要在之后将其展平。最好使用普通的旧数组变量,或者使用 reduce 如果你想变得花哨。

    var output = [];
    
    function processObject(path, obj) {
      path = path.concat([obj.name]);
      output.push({
        path: path.join("."),
        name: obj.name,
        uuid: obj.uuid,
      });
      var schema = objectSchemasList[obj.fieldObjectSchemaId];
      if (schema) {
        schema.forEach(processObject.bind(null, path));
      }
    }
    
    objectSchemaFields.forEach(processObject.bind(null, []));
    

    https://jsfiddle.net/m8t54bv5/

        2
  •  1
  •   Nina Scholz    6 年前

    function flat(p) {
        return function (r, { name, uuid, fieldObjectSchemaId }) {
            var path = p + (p && '.') + name;
            r.push({ path, name, uuid });
            return (objectSchemasList[fieldObjectSchemaId] || []).reduce(flat(path), r);
        };
    }
    
    var objectSchemasList = { 1: [{ name: 'list_field1_1', uuid: 'uuid1', fieldObjectSchemaId: 2 }, { name: 'list_field1_2', uuid: 'uuid2', fieldObjectSchemaId: null }], 2: [{ name: 'list_field2_1', uuid: 'uuid3', fieldObjectSchemaId: null }, { name: 'list_field2_2', uuid: 'uuid4', fieldObjectSchemaId: null }], 3: [{ name: 'list_field3_1', uuid: 'uuid5', fieldObjectSchemaId: 1 }, { name: 'list_field3_2', uuid: 'uuid6', fieldObjectSchemaId: null }] },
        objectSchemaFields = [{ name: 'field_1', uuid: '_uuid1', fieldObjectSchemaId: null }, { name: 'field_2', uuid: '_uuid2', fieldObjectSchemaId: null }, { name: 'field_3', uuid: '_uuid3', fieldObjectSchemaId: 1 }],
        result = objectSchemaFields.reduce(flat(''), []);
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }