Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to filter JSON data based on another JSON data in typescript

I have 2 JSON Data 1. Payers 2. Rules. I need to filter Payers JSON data based on PayerId from Rules JSON data.

{
"Payers": [
{
  "payerId": "12345",
  "name": "Test Payer1"     
},
{
  "payerId": "23456",
  "name": "Test Payer2",
 
},
{
  "payerId": "34567",
  "name": "Test Payer3"
}}

Rules JSON file

  {
  "Rules": [
 {
  "actions": {
    "canCopyRule": true       
  },
  "RuleId": 123,
  "description": "Test Rule",
  "isDisabled": false,
  "Criteria": [       
    {
      "autoSecondaryCriteriaId": 8888,
      "criteriaType": { "code": "primaryPayer", "value": "Primary Payer" },         
      "payerId": ["12345", "34567"]
    }
  ]
}
}]}

I need to filter Payers JSON data based on Rules JSON data if PayerID matches

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I need output like below

 {
 "Payers": [
 {
  "payerId": "12345",
  "name": "Test Payer1"     
},    
{
  "payerId": "34567",
  "name": "Test Payer3"
}
}

How to filter?

>Solution :

Code will check each rule and each critirias
and will return payers if payerId found in any of the given rules of any criteria

   
const payers = {
"Payers": [
{
  "payerId": "12345",
  "name": "Test Payer1"     
},
{
  "payerId": "23456",
  "name": "Test Payer2",
 
},
{
  "payerId": "34567",
  "name": "Test Payer3"
}]}
const rules = {
  "Rules": [
    {
      "actions": {
        "canCopyRule": true       
      },
    "RuleId": 123,
    "description": "Test Rule",
    "isDisabled": false,
    "Criteria": [       
      {
        "autoSecondaryCriteriaId": 8888,
        "criteriaType": { "code": "primaryPayer", "value": "Primary Payer" },         
        "payerId": ["12345", "34567"]
      }
    ]
  }
]
}

const data = payers.Payers.filter(payer => rules.Rules.findIndex(rule => rule.Criteria.findIndex(criteria => criteria.payerId.includes(payer.payerId)) != -1) !== -1)

console.log(data)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading