繁体   English   中英

展平嵌套对象和 Output 数组

[英]Flatten Nested Objects and Output Array

我有一个深度嵌套的 object 具有以下架构:

const data = {
  Car1: {
    key: "Car",
    value: "1",
    child: {
      Driver1: {
        key: "Driver",
        value: "1",
        child: {
          Trip1: {
            key: "Trip",
            value: 1,
            metrics: { distance: 1, time: 2 }
          }
        }
      },
      Driver2: {
        key: "Driver",
        value: "2",
        child: {
          Trip1: {
            key: "Trip",
            value: 1,
            metrics: { distance: 3, time: 4 }
          },
          Trip2: {
            key: "Trip",
            value: 2,
            metrics: { distance: 5, time: 6 }
          }
        }
      }
    }
  }
}

我需要展平为一个单一的对象数组,数组中的每个 object 都具有其直接子对象(ren)的所有属性。

每个嵌套的 object child对象都是具有属性keyvalue的对象的 Record。

嵌套结构中的最后一个 object 始终具有称为metrics的属性,该属性也应该展平为 object。

所以 output 看起来像:

[
{ Car: 1, Driver: 1, Trip: 1, distance: 1, time: 2 },
{ Car: 1, Driver: 2, Trip: 1, distance: 3, time: 4 },
{ Car: 1, Driver: 2, Trip: 2, distance: 5, time: 6 }
]

我尝试了以下代码,但它仅捕获子树中的一级深度:

  private flattenOutput(child: Record<string, OutputChild> = this.output): any[] {
    console.log('flattening', Object.keys(child));
    return Object.values(child).map((child) => {
      return Object.assign(
        {},
        { [child.key]: child.value },
        ...this.flattenOutput(child.child),
        child.metrics || {},
      );
    }, {});
  }

通过拥有正确的嵌套对象,您可以采用递归方法并收集键/值并返回包含所需对象的平面数组。

 const collect = (object, temp = {}) => Object.values(object).flatMap(({ key, value, child, metrics }) => child? collect(child, {...temp, [key]: value }): {...temp, [key]: value, ...metrics } ), data = { Car1: { key: "Car", value: "1", child: { Driver1: { key: "Driver", value: "1", child: { Trip1: { key: "Trip", value: 1, metrics: { distance: 1, time: 2 } } } }, Driver2: { key: "Driver", value: "2", child: { Trip1: { key: "Trip", value: 1, metrics: { distance: 3, time: 4 } }, Trip2: { key: "Trip", value: 2, metrics: { distance: 5, time: 6 } } } } } } }, result = collect(data); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

我会这样做:

 data = { Car1: { key: "Car", value: "1", child: { Driver1: { key: "Driver", value: "1", child: { Trip1: { key: "Trip", value: 1, metrics: { distance: 1, time: 2} } } }, Driver2: { key: "Driver", value: "2", child: { Trip1: { key: "Trip", value: 1, metrics: { distance: 3, time: 4} }, Trip2: { key: "Trip", value: 2, metrics: { distance: 5, time: 6} } } } } } } let result = [] for (let Car in data ) for (let Driver in data[Car].child) for (let Trip in data[Car].child[Driver].child) result.push( { Car: data[Car].value, Driver: data[Car].child[Driver].value, Trip: data[Car].child[Driver].child[Trip].value, distance: data[Car].child[Driver].child[Trip].metrics.distance, time: data[Car].child[Driver].child[Trip].metrics.time }) console.log( result )
 .as-console-wrapper { max-height: 100%;important: top; 0; }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM