简体   繁体   中英

How to filter nested array in typescript?

I have a JSON as below

{
  "Header": {
    "Time": "2020-06-09T07:03:20-07:00",
    "ReportName": "JournalReport",
    "StartPeriod": "2020-06-09",
    "EndPeriod": "2020-06-09",
    "Currency": "USD",
    "Option": [
      {
        "Name": "NoReportData",
        "Value": "false"
      }
    ]
  },
  "Columns": {
    "Column": [
      {
        "ColTitle": "Date",
        "ColType": "Date",
        "MetaData": [
          {
            "Name": "ColKey",
            "Value": "tx_date"
          }
        ]
      },
      {
        "ColTitle": "Transaction Type",
        "ColType": "String",
        "MetaData": [
          {
            "Name": "ColKey",
            "Value": "txn_type"
          }
        ]
      },
      {
        "ColTitle": "Num",
        "ColType": "String",
        "MetaData": [
          {
            "Name": "ColKey",
            "Value": "doc_num"
          }
        ]
      },
      {
        "ColTitle": "Name",
        "ColType": "String",
        "MetaData": [
          {
            "Name": "ColKey",
            "Value": "name"
          }
        ]
      },
      {
        "ColTitle": "Memo/Description",
        "ColType": "String",
        "MetaData": [
          {
            "Name": "ColKey",
            "Value": "memo"
          }
        ]
      },
      {
        "ColTitle": "Account",
        "ColType": "String",
        "MetaData": [
          {
            "Name": "ColKey",
            "Value": "account_name"
          }
        ]
      },
      {
        "ColTitle": "Debit",
        "ColType": "Money",
        "MetaData": [
          {
            "Name": "ColKey",
            "Value": "debt_home_amt"
          }
        ]
      },
      {
        "ColTitle": "Credit",
        "ColType": "Money",
        "MetaData": [
          {
            "Name": "ColKey",
            "Value": "credit_home_amt"
          }
        ]
      }
    ]
  },
  "Rows": {
    "Row": [
      {
        "ColData": [
          {
            "value": "0-00-00"
          },
          {
            "value": "",
            "id": "559"
          },
          {
            "value": ""
          },
          {
            "value": "",
            "id": ""
          },
          {
            "value": ""
          },
          {
            "value": "California Department of Tax and Fee Administration Payable",
            "id": "678"
          },
          {
            "value": ""
          },
          {
            "value": "1.25"
          }
        ],
        "type": "Data"
      },
      {
        "ColData": [
          {
            "value": "0-00-00"
          },
          {
            "value": "",
            "id": "559"
          },
          {
            "value": ""
          },
          {
            "value": "",
            "id": ""
          },
          {
            "value": ""
          },
          {
            "value": "California Department of Tax and Fee Administration Payable",
            "id": "678"
          },
          {
            "value": ""
          },
          {
            "value": ".10"
          }
        ],
        "type": "Data"
      },
      {
        "Summary": {
          "ColData": [
            {
              "value": ""
            },
            {
              "value": ""
            },
            {
              "value": ""
            },
            {
              "value": ""
            },
            {
              "value": ""
            },
            {
              "value": ""
            },
            {
              "value": "31.75"
            },
            {
              "value": "31.75"
            }
          ]
        },
        "type": "Section"
      },
      {
        "ColData": [
          {
            "value": "0-00-00"
          },
          {
            "value": "",
            "id": "567"
          },
          {
            "value": ""
          },
          {
            "value": "",
            "id": ""
          },
          {
            "value": ""
          },
          {
            "value": "Accounts Payable (A/P)",
            "id": "676"
          },
          {
            "value": "232.00"
          },
          {
            "value": ""
          }
        ],
        "type": "Data"
      }
    ]
  }
}

I want to filter this array which matches Rows.Row[someindex].ColData[1].id == 567 . Here id should match with second object of ColData array.

But after filtering i don't want to skip the records from resulting array those have Summary as Rows.Row[someindex].Summary . Summary records should be included in resulting array.

I have tried below code but it does not work because In Row.Row array Summary can be there instead of ColData at some indexs & also i want include Summary records in resulting array even if i have found the desired object.

function getResult(filterbY, objList) {
  return objList.Rows.Row.filter(function(obj) {
   return obj.ColData.some(function(item){
     return item.id == filterbY;
   });
 });
}

Please help with the same?

you need to include your summary in your filter function then,

function getResult(filterbY, objList) {
  return objList.Rows.Row.filter(function(obj) {
   if(obj.Summary){return true;}
   return obj.ColData.some(function(item){
     return item.id == filterbY;
   });
 });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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