代码之家  ›  专栏  ›  技术社区  ›  thomas-peter

我该如何转换这个?

  •  0
  • thomas-peter  · 技术社区  · 8 年前

    我想找到一个很好的优雅的解决方案来进行下面的转换。如何以最简单的方式(可选地使用功能库ramda)实现这一点?

    const data = [
      {
        id: 'a',
      },
      {
        id: 'b',
        routes: [
          {
            id: 'c',
          },
          {
            id: 'd',
            routes: [
              {
                id: 'e',
              }
            ]
          }
        ]
      }
    ];
    
    const expected = [
      {
        id: 'a',
      },
      {
        id: 'b',
      },
      {
        id: 'c',
      },
      {
        id: 'd',
      },
      {
        id: 'e',
      }
    ];
    
    3 回复  |  直到 8 年前
        1
  •  2
  •   CertainPerformance    8 年前

    reduce

    const data = [
      {
        id: 'a',
      },
      {
        id: 'b',
        routes: [
          {
            id: 'c',
          },
          {
            id: 'd',
            routes: [
              {
                id: 'e',
              }
            ]
          }
        ]
      }
    ];
    
    const transform = arr => arr.reduce((a, { id, routes }) => (
      [...a, { id }, ...transform(routes || [])]
    ), []);
    console.log(transform(data));

    const data = [{
        id: 'a',
      },
      {
        id: 'b',
        routes: [{
            id: 'c',
          },
          {
            id: 'd',
            routes: [{
              id: 'e',
            }]
          }
        ]
      }
    ];
    
    const transform = (arr, pushTo = []) => arr.reduce((a, { id, routes }) => {
      a.push({ id });
      if (routes) transform(routes, a);
      return a;
    }, pushTo);
    console.log(transform(data));

    routes

    const data = [{
        id: 'a',
      },
      {
        id: 'b',
        routes: [{
            id: 'c',
          },
          {
            id: 'd',
            routes: [{
              id: 'e',
            }]
          }
        ]
      }
    ];
    
    const transform = (arr, prop, pushTo = []) => arr.reduce((a, item) => {
      const nested = item[prop];
      a.push({ id: item.id });
      if (nested) transform(nested, prop, a);
      return a;
    }, pushTo);
    console.log(transform(data, 'routes'));
        2
  •  1
  •   Nina Scholz    8 年前

    function getFlat(array = []) {
        return array.reduce((r, { id, routes }) => r.concat({ id }, getFlat(routes)), []);
    }
    
    const data = [{ id: 'a', }, { id: 'b', routes: [{ id: 'c', }, { id: 'd', routes: [{ id: 'e', }] }] }];
    
    console.log(getFlat(data));
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    routes

    function getFlat(array = []) {
        return array.reduce((r, { routes, ...o }) => r.concat(o, getFlat(routes)), []);
    }
    
    const data = [{ id: 'a', foo: 1 }, { id: 'b', routes: [{ id: 'c' }, { id: 'd', routes: [{ id: 'e', bar: 2 }] }] }];
    
    console.log(getFlat(data));
        3
  •  1
  •   Scott Christopher    8 年前

    R.chain

    const fn = R.chain(({routes = [], ...rest}) => [{...rest}, ...fn(routes)])
    

    Array.prototype.flatMap

    const fn = (xs = []) => xs.flatMap(({routes, ...rest}) => [{...rest}, ...fn(routes)])