简体   繁体   English

Angular 8 Typescript 展平嵌套数组

[英]Angular 8 Typescript flatten nested array

I have nested JSON data that I need to flatten in order to display all options to user in a table without dropdowns.我已经嵌套了 JSON 数据,我需要将这些数据展平以便在没有下拉列表的表格中向用户显示所有选项。

The data looks something like this...数据看起来像这样......

0:
ID: 4
Date: "2019-08-09T00:00:00"
Data:
Year: 2019
Identification: "F31073"
nestedArray: Array(1)
   0:
      array1ID: "31"
      array1Code: "073"
      nestedArray2: Array(4)
      0:
        array2info: 24
        nestedArray3: Array(8)
          0:
          array3Identifier: "ECF5EE44-50E1-4F07-AB38-CF7A60A6276F"
          array3Number: 31
          arrayThreeIndicator: "N"
          nestedArray4: Array(1)
             0:
             array4Info: "31"
             array4Code: "073"

I've looked at many solutions but none have given me back a flat array of data.我查看了许多解决方案,但没有一个给我返回一个平面数据数组。

UPDATE here is the JSON data that I am trying to flatten so that for each nested array the fields before it are duplicated and all of the data is visible with no nested arrays.这里的更新是 JSON 数据,我试图将其展平,以便对于每个嵌套数组,复制之前的字段并且所有数据都是可见的,没有嵌套的 arrays。

"[{"FileCustomerIdentifier":4,"TransmissionDate":"2019-08- 
 09T00:00:00","TransmissionData":"{\r\n  \"cropYear\": 2019,\r\n  
 \"originatorIdentification\": \"F31073\",\r\n  \"farm\": [\r\n    {\r\n      
 \"administrativeStateFsaCode\": \"31\",\r\n      
 \"administrativeCountyFsaCode\": \"073\",\r\n      \"farmNumber\": 
  4609,\r\n      
 \"tract\": [\r\n        {\r\n          \"tractNumber\": 24,\r\n          
 \"field\": [\r\n            {\r\n              \"cluIdentifier\": 
 \"ECF5EE44-50E1-4F07-AB38-CF7A60A6276F\",\r\n              
 \"fieldNumber\": 
 31,\r\n              \"cluProducerReviewRequestIndicator\": \"N\",\r\n              
 \"agriculturalProductionPlan\": [\r\n                {\r\n                  
 \"stateAnsiCode\": \"31\",\r\n                  \"countyAnsiCode\": 
 \"073\",\r\n                  \"coreProductCode\": \"0102\",\r\n                  
 \"coreProductTypeCode\": \"0794\",\r\n                  
 \"productIntendedUseCode\": \"0007\",\r\n                  
 \"originalReportedAcreage\": 109.93,\r\n                  
 \"finalReportedAcreage\": 109.93,\r\n                  
 \"reportedAcreageModifiedIndicator\": \"N\",\r\n                  
 \"plantedDateModifiedIndicator\": \"N\",\r\n                  
 \"productPlantingCode\": null,\r\n                  
 \"irrigationPracticeCode\": \"N\",\r\n                  
 \"organicPracticeTypeCode\": \"N\",\r\n                  
 \"croppingPracticeCode\": 997,\r\n                  \"producerShare\": 
 [\r\n                    
 {\r\n                      \"fileCustomerIdentifier\": 2,\r\n                      
 \"producerSharePercent\": 1\r\n                    }\r\n                  
 ]\r\n                }\r\n              ]\r\n            },\r\n            
 {\r\n              \"cluIdentifier\": \"5E83C2F1-6399-46DD-8695- 
 90CEAAF02721\",\r\n              \"fieldNumber\": 32,\r\n              
 \"cluProducerReviewRequestIndicator\": \"N\",\r\n              
 \"agriculturalProductionPlan\": [\r\n                {\r\n                  
 \"stateAnsiCode\": \"31\",\r\n                  \"countyAnsiCode\": 
 \"073\",\r\n                  \"coreProductCode\": \"0041\",\r\n                  
 \"coreProductTypeCode\": \"0964\",\r\n                  
 \"productIntendedUseCode\": \"0016\",\r\n                  
 \"originalReportedAcreage\": 43.73,\r\n                  
 \"finalReportedAcreage\": 43.73,\r\n                  
 \"reportedAcreageModifiedIndicator\": \"N\",\r\n                  
 \"originalPlantedDate\": \"2019-05-02\",\r\n                  
 \"finalPlantedDate\": \"2019-05-02\",\r\n                  
 \"plantedDateModifiedIndicator\": \"N\",\r\n                  
 \"productPlantingCode\": null,\r\n                  
 \"irrigationPracticeCode\": \"N\",\r\n                  
 \"organicPracticeTypeCode\": \"N\",\r\n                  
 \"croppingPracticeCode\": 997,\r\n                  \"producerShare\": 
 [\r\n                    
 {\r\n                      \"fileCustomerIdentifier\": 4,\r\n                      
 \"producerSharePercent\": 0.5\r\n                    },\r\n                    
 {\r\n                      \"fileCustomerIdentifier\": 5,\r\n                      
 \"producerSharePercent\": 0.5\r\n                    }\r\n                  
 ]\r\n                }\r\n              ]\r\n            },\r\n            
 {\r\n              \"cluIdentifier\": \"0E021A77-6BA2-4FAA-A522- 
 A1F383C0FBF5\",\r\n              \"fieldNumber\": 29,\r\n              
 \"cluProducerReviewRequestIndicator\": \"N\",\r\n              
 \"agriculturalProductionPlan\": [\r\n                {\r\n                  
 \"stateAnsiCode\": \"31\",\r\n                  \"countyAnsiCode\": 
 \"073\",\r\n                  \"coreProductCode\": \"0102\",\r\n                  
 \"coreProductTypeCode\": \"0794\",\r\n                  
 \"productIntendedUseCode\": \"0007\",\r\n                  
 \"originalReportedAcreage\": 11.77,\r\n                  
 \"finalReportedAcreage\": 11.77,\r\n                  
 \"reportedAcreageModifiedIndicator\": \"N\",\r\n                  
 \"plantedDateModifiedIndicator\": \"N\",\r\n                  
 \"productPlantingCode\": null,\r\n                  
 \"irrigationPracticeCode\": \"N\",\r\n                  
 \"organicPracticeTypeCode\": \"N\",\r\n                  
 \"croppingPracticeCode\": 997,\r\n                  \"producerShare\": 
 [\r\n                    
 {\r\n                      \"fileCustomerIdentifier\": 2,\r\n                      
 \"producerSharePercent\": 1\r\n                    }\r\n                  
 ]\r\n                }\r\n              ]\r\n            },\r\n            
 {\r\n              \"cluIdentifier\": \"BFE94518-7B5E-40A3-BF71- 
 0EA6B932C3F9\",\r\n              \"fieldNumber\": 28,\r\n              
 \"cluProducerReviewRequestIndicator\": \"N\",\r\n              
 \"agriculturalProductionPlan\": [\r\n                {\r\n                  
 \"stateAnsiCode\": \"31\",\r\n                  \"countyAnsiCode\": 
 \"073\",\r\n                  \"subfieldNumber\": \"A\",\r\n                  
 \"coreProductCode\": \"0102\",\r\n                  
 \"coreProductTypeCode\": 
 \"0794\",\r\n                  \"productIntendedUseCode\": \"0007\",\r\n                  
 \"originalReportedAcreage\": 52.0,\r\n                  
 \"finalReportedAcreage\": 52.0,\r\n                  
 \"reportedAcreageModifiedIndicator\": \"N\",\r\n                  
 \"plantedDateModifiedIndicator\": \"N\",\r\n                  
 \"productPlantingCode\": null,\r\n                  
 \"irrigationPracticeCode\": \"N\",\r\n                  
 \"organicPracticeTypeCode\": \"N\",\r\n                  
 \"croppingPracticeCode\": 997,\r\n                  \"producerShare\": 
  [\r\n                    
 {\r\n                      \"fileCustomerIdentifier\": 2,\r\n                      
 \"producerSharePercent\": 1\r\n                    }\r\n                  
 ]\r\n                }\r\n              ]\r\n            },\r\n            
 {\r\n              \"cluIdentifier\": \"9ac10b70-0d9d-4a38-97f7- 
 22e63a3c0f9d\",\r\n              \"fieldNumber\": 2,\r\n              
 \"cluProducerReviewRequestIndicator\": \"N\",\r\n              
 \"agriculturalProductionPlan\": [\r\n                {\r\n                  
 \"stateAnsiCode\": \"31\",\r\n                  \"countyAnsiCode\": 
 \"073\",\r\n                  \"subfieldNumber\": \"A\",\r\n                  
 \"coreProductCode\": \"0102\",\r\n                  
 \"coreProductTypeCode\": 
 \"0794\",\r\n                  \"productIntendedUseCode\": \"0013\",\r\n                  
 \"originalReportedAcreage\": 0.22,\r\n                  
 \"finalReportedAcreage\": 0.22,\r\n                  
 \"reportedAcreageModifiedIndicator\": \"N\",\r\n                  
 \"plantedDateModifiedIndicator\": \"N\",\r\n                  
 \"productPlantingCode\": null,\r\n                  
 \"irrigationPracticeCode\": \"N\",\r\n                  
 \"organicPracticeTypeCode\": \"N\",\r\n                  
 \"croppingPracticeCode\": 997,\r\n                  \"producerShare\": 
 [\r\n                    
 {\r\n                      \"fileCustomerIdentifier\": 2,\r\n                      
 \"producerSharePercent\": 1\r\n                    }\r\n                  
 ]\r\n                },\r\n                {\r\n                  
 \"stateAnsiCode\": \"31\",\r\n                  \"countyAnsiCode\": 
 \"073\",\r\n                  \"subfieldNumber\": \"B\",\r\n                  
 \"coreProductCode\": \"0041\",\r\n                  
 \"coreProductTypeCode\": 
 \"0964\",\r\n                  \"productIntendedUseCode\": \"0016\",\r\n                  
 \"originalReportedAcreage\": 18.58,\r\n                  
 \"finalReportedAcreage\": 18.58,\r\n                  
 \"reportedAcreageModifiedIndicator\": \"N\",\r\n                  
 \"originalPlantedDate\": \"2019-05-02\",\r\n                  
 \"finalPlantedDate\": \"2019-05-02\",\r\n                  
 \"plantedDateModifiedIndicator\": \"N\",\r\n                  
 \"productPlantingCode\": null,\r\n                  
 \"irrigationPracticeCode\": \"N\",\r\n                  
 \"organicPracticeTypeCode\": \"N\",\r\n                  
 \"croppingPracticeCode\": 997,\r\n                  \"producerShare\": 
 [\r\n                    
 {\r\n                      \"fileCustomerIdentifier\": 5,\r\n                      
 \"producerSharePercent\": 0.5\r\n                    },\r\n                    
 {\r\n                      \"fileCustomerIdentifier\": 4,\r\n                      
 \"producerSharePercent\": 0.5\r\n                    }\r\n                  
 ]\r\n                }\r\n              ]\r\n            },\r\n            
 {\r\n              \"cluIdentifier\": \"940ebc4b-fdc9-4cae-b9af- 
 b0b360afba5f\",\r\n              \"fieldNumber\": 4,\r\n              
 \"cluProducerReviewRequestIndicator\": \"N\",\r\n              
 \"agriculturalProductionPlan\": [\r\n                {\r\n                  
 \"stateAnsiCode\": \"31\",\r\n                  \"countyAnsiCode\": 
 \"073\",\r\n                  \"subfieldNumber\": \"A\",\r\n                  
 \"coreProductCode\": \"0041\",\r\n                  
 \"coreProductTypeCode\": 
 \"0964\",\r\n                  \"productIntendedUseCode\": \"0016\",\r\n                  
 \"originalReportedAcreage\": 10.64,\r\n                  
 \"finalReportedAcreage\": 10.64,\r\n                  
 \"reportedAcreageModifiedIndicator\": \"N\",\r\n                  
 \"originalPlantedDate\": \"2019-05-02\",\r\n                  
 \"finalPlantedDate\": \"2019-05-02\",\r\n                  
 \"plantedDateModifiedIndicator\": \"N\",\r\n                  
 \"productPlantingCode\": null,\r\n                  
 \"irrigationPracticeCode\": \"N\",\r\n                  
 \"organicPracticeTypeCode\": \"N\",\r\n                  
 \"croppingPracticeCode\": 997,\r\n                  \"producerShare\": [\r\n                    
 {\r\n                      \"fileCustomerIdentifier\": 4,\r\n                      
 \"producerSharePercent\": 0.5\r\n                    },\r\n                    
 {\r\n                      \"fileCustomerIdentifier\": 5,\r\n                      
 \"producerSharePercent\": 0.5\r\n                    }\r\n    …"

Any help is greatly appreciated!任何帮助是极大的赞赏!

If your nested array keys are the same, you can do it recursively:如果您的嵌套数组键相同,则可以递归执行:

type NestedData = { nested: NestedData[] }[]

function flatten(nestedData: NestedData) {
  let flattened = [];

  for (const { nested, ...restData } of nestedData) {
    flattened.push(restData);
    if (!nested || !nested.length) continue;
    flattened = flattened.concat(flatten(nested))
  }

  return flattened;
}  

const flattened = flatten(yourData);

You get the idea.你明白了。

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

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