Home > other >  How can I remove parts from nested object array with reduce?
How can I remove parts from nested object array with reduce?

Time:10-13

I am working on a React Project and need to flatten/cut out the relevant and needed information from an object array.

this is how it look likes: whole array Under "Person" there is a nested array called "Icds" where there should be exactly one leading diagnosis (=true). How can I filter or reduce this huge array, so I get a similar/new one without too much data inside "Icds" or only one "Icd" where leadingDiagnosis equals true?

Here is one example entry out of 0 to 7:

structure

{
    "_id": "611cd9eae20bd167a796f884",
    "BeforeAccJob": "Antennenbauer/-in",
    "FuneralCost": 6,
    "FuneralCostWithSeverancePay": false,
    "FuneralCostSeverancePayDuration": 1,
    "FuneralCostDynamisationRate": 0.025,
    "FuneralCostCapitalisationRate": 0.025,
    "FuneralCostCostSharingAgreement": {
        "Active": false,
        "Rate": 1,
        "Limit": 0,
        "Unlimited": false,
        "__typename": "CostSharingAgreement"
    },
    "Miscellaneous": 300000.5,
    "MiscellaneousWithSeverancePay": false,
    "MiscellaneousSeverancePayDuration": 1,
    "MiscellaneousCapitalisationRate": 0.025,
    "MiscellaneousDynamisationRate": 0.025,
    "MiscellaneousCostSharingAgreement": {
        "Active": false,
        "Rate": 1,
        "Limit": 0,
        "Unlimited": false,
        "__typename": "CostSharingAgreement"
    },
    "__typename": "CalculationEntity",
    "Person": {
        "_id": "P30081568",
        "Firstname": "Nick",
        "Lastname": "Fury",
        "Birthday": "1976-10-10T00:00:00.000Z",
        "MaritalStatus": "NotSet",
        "Icds": [
            {
                "_id": 85071,
                "Mk": 3,
                "Code": "S72.0",
                "ActineoText": "Schenkelhalsfraktur",
                "Text": "Schenkelhalsfraktur",
                "Status": "assured",
                "IsLeading": false,
                "__typename": "ProcessICD"
            },
            {
                "_id": 85072,
                "Mk": 2,
                "Code": "T01.9",
                "ActineoText": "Multiple offene Wunden, nicht näher bezeichnet",
                "Text": "Multiple offene Wunden, nicht näher bezeichnet",
                "Status": "assured",
                "IsLeading": false,
                "__typename": "ProcessICD"
            },
            {
                "_id": 85073,
                "Mk": 3,
                "Code": "T08.0",
                "ActineoText": "Fraktur der Wirbelsäule",
                "Text": "Fraktur der Wirbelsäule, Höhe nicht näher bezeichnet: geschlossen",
                "Status": "assured",
                "IsLeading": false,
                "__typename": "ProcessICD"
            },
            {
                "_id": 85074,
                "Mk": 1,
                "Code": "J93.9",
                "ActineoText": "Pneumothorax",
                "Text": "Pneumothorax, nicht näher bezeichnet",
                "Status": "assured",
                "IsLeading": false,
                "__typename": "ProcessICD"
            },
            {
                "_id": 634234,
                "Mk": 1,
                "Code": "Z96.64",
                "ActineoText": "Vorhandensein einer Hüftgelenkprothese",
                "Text": "Vorhandensein einer Hüftgelenkprothese",
                "Status": "unknown",
                "IsLeading": false,
                "__typename": "ProcessICD"
            },
            {
                "_id": 634235,
                "Mk": 1,
                "Code": "M87.95",
                "ActineoText": "Knochennekrose, nicht näher bezeichnet: Beckenregion und Oberschenkel",
                "Text": "Knochennekrose, nicht näher bezeichnet: Beckenregion und Oberschenkel",
                "Status": "unknown",
                "IsLeading": false,
                "__typename": "ProcessICD"
            },
            {
                "_id": 387916,
                "Mk": 3,
                "Code": "S32.01",
                "ActineoText": null,
                "Text": "Fraktur eines Lendenwirbels: L1",
                "Status": "unknown",
                "IsLeading": false,
                "__typename": "ProcessICD"
            },
            {
                "_id": 387917,
                "Mk": 3,
                "Code": "S32.02",
                "ActineoText": null,
                "Text": "Fraktur eines Lendenwirbels: L2",
                "Status": "unknown",
                "IsLeading": false,
                "__typename": "ProcessICD"
            },
            {
                "_id": 610262,
                "Mk": 3,
                "Code": "S12.24",
                "ActineoText": "Fraktur des 6. Halswirbels",
                "Text": "Fraktur des 6. Halswirbels",
                "Status": "unknown",
                "IsLeading": true,
                "__typename": "ProcessICD"
            },
            {
                "_id": 387909,
                "Mk": 3,
                "Code": "S72.3",
                "ActineoText": null,
                "Text": "Fraktur des Femurschaftes",
                "Status": "unknown",
                "IsLeading": false,
                "__typename": "ProcessICD"
            },
            {
                "_id": 387908,
                "Mk": 4,
                "Code": "S71.88",
                "ActineoText": null,
                "Text": "Weichteilschaden II. Grades bei offener Fraktur oder Luxation der Hüfte und des Oberschenkels",
                "Status": "unknown",
                "IsLeading": true,
                "__typename": "ProcessICD"
            },
            {
                "_id": 385884,
                "Mk": 1,
                "Code": "S71.1",
                "ActineoText": null,
                "Text": "Offene Wunde des Oberschenkels",
                "Status": "unknown",
                "IsLeading": false,
                "__typename": "ProcessICD"
            },
            {
                "_id": 387911,
                "Mk": 3,
                "Code": "S82.18",
                "ActineoText": null,
                "Text": "Fraktur des proximalen Endes der Tibia: Sonstige",
                "Status": "unknown",
                "IsLeading": false,
                "__typename": "ProcessICD"
            },
            {
                "_id": 387910,
                "Mk": 3,
                "Code": "S81.88",
                "ActineoText": null,
                "Text": "Weichteilschaden II. Grades bei offener Fraktur oder Luxation des Unterschenkels",
                "Status": "unknown",
                "IsLeading": false,
                "__typename": "ProcessICD"
            },
            {
                "_id": 385887,
                "Mk": 1,
                "Code": "S81.0",
                "ActineoText": null,
                "Text": "Offene Wunde des Knies",
                "Status": "unknown",
                "IsLeading": false,
                "__typename": "ProcessICD"
            },
            {
                "_id": 385888,
                "Mk": 3,
                "Code": "S82.38",
                "ActineoText": null,
                "Text": "Distale Fraktur der Tibia: Sonstige",
                "Status": "unknown",
                "IsLeading": false,
                "__typename": "ProcessICD"
            },
            {
                "_id": 385889,
                "Mk": 3,
                "Code": "S82.5",
                "ActineoText": null,
                "Text": "Fraktur des Innenknöchels",
                "Status": "unknown",
                "IsLeading": false,
                "__typename": "ProcessICD"
            },
            {
                "_id": 387905,
                "Mk": 3,
                "Code": "S61.87",
                "ActineoText": null,
                "Text": "Weichteilschaden I. Grades bei offener Fraktur oder Luxation des Handgelenkes und der Hand",
                "Status": "unknown",
                "IsLeading": false,
                "__typename": "ProcessICD"
            },
            {
                "_id": 387906,
                "Mk": 2,
                "Code": "S62.31",
                "ActineoText": null,
                "Text": "Fraktur eines sonstigen Mittelhandknochens: Basis",
                "Status": "unknown",
                "IsLeading": false,
                "__typename": "ProcessICD"
            },
            {
                "_id": 387907,
                "Mk": 2,
                "Code": "S63.2",
                "ActineoText": null,
                "Text": "Multiple Luxationen der Finger",
                "Status": "unknown",
                "IsLeading": false,
                "__typename": "ProcessICD"
            },
            {
                "_id": 387912,
                "Mk": 3,
                "Code": "S82.31",
                "ActineoText": null,
                "Text": "Distale Fraktur der Tibia: Mit Fraktur der Fibula (jeder Teil)",
                "Status": "unknown",
                "IsLeading": false,
                "__typename": "ProcessICD"
            },
            {
                "_id": 387913,
                "Mk": 3,
                "Code": "S22.44",
                "ActineoText": null,
                "Text": "Rippenserienfraktur: Mit Beteiligung von vier und mehr Rippen",
                "Status": "unknown",
                "IsLeading": false,
                "__typename": "ProcessICD"
            },
            {
                "_id": 387914,
                "Mk": 3,
                "Code": "S22.20",
                "ActineoText": null,
                "Text": "Fraktur des Sternums: Nicht näher bezeichnet",
                "Status": "unknown",
                "IsLeading": false,
                "__typename": "ProcessICD"
            },
            {
                "_id": 387918,
                "Mk": 2,
                "Code": "D62",
                "ActineoText": null,
                "Text": "Akute Blutungsanämie",
                "Status": "unknown",
                "IsLeading": false,
                "__typename": "ProcessICD"
            }
        ],
        "__typename": "Person"
    },
    "Blocks": [
        {
            "Name": "PropertyDamageCost",
            "IsRounded": false,
            "Coefficient": 1,
            "Sum": 1390,
            "WithSeverancePay": false,
            "SeverancePayDuration": 1,
            "CapitalisationRate": 0.025,
            "DynamisationRate": 1,
            "CostSharingAgreement": {
                "Active": false,
                "Rate": 1,
                "Limit": 0,
                "Unlimited": false,
                "__typename": "CostSharingAgreement"
            },
            "__typename": "CalculationBlock"
        },
        {
            "Name": "HealingTreatmentCost",
            "IsRounded": false,
            "Coefficient": 1,
            "Sum": 200,
            "WithSeverancePay": false,
            "SeverancePayDuration": 1,
            "CapitalisationRate": 0.025,
            "DynamisationRate": 1,
            "CostSharingAgreement": {
                "Active": false,
                "Rate": 1,
                "Limit": 0,
                "Unlimited": false,
                "__typename": "CostSharingAgreement"
            },
            "__typename": "CalculationBlock"
        },
        {
            "Name": "IncomeCost",
            "IsRounded": false,
            "Coefficient": 1,
            "Sum": 0,
            "WithSeverancePay": null,
            "SeverancePayDuration": null,
            "CapitalisationRate": 0.025,
            "DynamisationRate": 1,
            "CostSharingAgreement": {
                "Active": false,
                "Rate": 1,
                "Limit": 0,
                "Unlimited": false,
                "__typename": "CostSharingAgreement"
            },
            "__typename": "CalculationBlock"
        },
        {
            "Name": "HousekeepingDamageCost",
            "IsRounded": false,
            "Coefficient": 1,
            "Sum": 0,
            "WithSeverancePay": false,
            "SeverancePayDuration": 1,
            "CapitalisationRate": 0.025,
            "DynamisationRate": 1,
            "CostSharingAgreement": {
                "Active": false,
                "Rate": 1,
                "Limit": 0,
                "Unlimited": false,
                "__typename": "CostSharingAgreement"
            },
            "__typename": "CalculationBlock"
        },
        {
            "Name": "IncreasedNeeds",
            "IsRounded": false,
            "Coefficient": 1,
            "Sum": 0,
            "WithSeverancePay": false,
            "SeverancePayDuration": 1,
            "CapitalisationRate": 0.025,
            "DynamisationRate": 1,
            "CostSharingAgreement": {
                "Active": false,
                "Rate": 1,
                "Limit": 0,
                "Unlimited": false,
                "__typename": "CostSharingAgreement"
            },
            "__typename": "CalculationBlock"
        },
        {
            "Name": "DamageCost",
            "IsRounded": false,
            "Coefficient": 1,
            "Sum": 0,
            "WithSeverancePay": false,
            "SeverancePayDuration": 1,
            "CapitalisationRate": 0.025,
            "DynamisationRate": 1,
            "CostSharingAgreement": {
                "Active": false,
                "Rate": 1,
                "Limit": 0,
                "Unlimited": false,
                "__typename": "CostSharingAgreement"
            },
            "__typename": "CalculationBlock"
        },
        {
            "Name": "SurvivorsPensionCost",
            "IsRounded": false,
            "Coefficient": 1,
            "Sum": 0,
            "WithSeverancePay": false,
            "SeverancePayDuration": 1,
            "CapitalisationRate": 0.025,
            "DynamisationRate": 1,
            "CostSharingAgreement": {
                "Active": false,
                "Rate": 1,
                "Limit": 0,
                "Unlimited": false,
                "__typename": "CostSharingAgreement"
            },
            "__typename": "CalculationBlock"
        },
        {
            "Name": "MaintenanceDamageCost",
            "IsRounded": false,
            "Coefficient": 1,
            "Sum": 0,
            "WithSeverancePay": false,
            "SeverancePayDuration": 1,
            "CapitalisationRate": 0.025,
            "DynamisationRate": 1,
            "CostSharingAgreement": {
                "Active": false,
                "Rate": 1,
                "Limit": 0,
                "Unlimited": false,
                "__typename": "CostSharingAgreement"
            },
            "__typename": "CalculationBlock"
        }
    ]
}

CodePudding user response:

Icds array with just the IsLeading and _id fields (add more as you see fit):

const icds = calculations.Person.Icds.map((icd) => ({ isLeading, _id }));

Just the Icds object where IsLeading is true. Note that if there are multiple objects where this condition holds, only the first one is returned.

const isLeadingIcd = calculations.Person.Icds.find((icd) => icd.IsLeading === true);

Is this what you're looking for? If not I think it's best you provide an example of the final result you're looking for.

CodePudding user response:

Here is the solution. The steps arre the followings:

  1. filtering out the true valued Icds nested objects where the IsLeading is true.
  2. Createing a new Object clone where the data.persons.Icds values are replaced with the filtered array.

If you need the first true valued element you can use find againt filter. Or other condition in the filter.

const data = {
  _id: "611cd9eae20bd167a796f884",
  BeforeAccJob: "Antennenbauer/-in",
  FuneralCost: 6,
  FuneralCostWithSeverancePay: false,
  FuneralCostSeverancePayDuration: 1,
  FuneralCostDynamisationRate: 0.025,
  FuneralCostCapitalisationRate: 0.025,
  FuneralCostCostSharingAgreement: {
    Active: false,
    Rate: 1,
    Limit: 0,
    Unlimited: false,
    __typename: "CostSharingAgreement",
  },
  Miscellaneous: 300000.5,
  MiscellaneousWithSeverancePay: false,
  MiscellaneousSeverancePayDuration: 1,
  MiscellaneousCapitalisationRate: 0.025,
  MiscellaneousDynamisationRate: 0.025,
  MiscellaneousCostSharingAgreement: {
    Active: false,
    Rate: 1,
    Limit: 0,
    Unlimited: false,
    __typename: "CostSharingAgreement",
  },
  __typename: "CalculationEntity",
  Person: {
    _id: "P30081568",
    Firstname: "Nick",
    Lastname: "Fury",
    Birthday: "1976-10-10T00:00:00.000Z",
    MaritalStatus: "NotSet",
    Icds: [
      {
        _id: 85071,
        Mk: 3,
        Code: "S72.0",
        ActineoText: "Schenkelhalsfraktur",
        Text: "Schenkelhalsfraktur",
        Status: "assured",
        IsLeading: false,
        __typename: "ProcessICD",
      },
      {
        _id: 85072,
        Mk: 2,
        Code: "T01.9",
        ActineoText: "Multiple offene Wunden, nicht näher bezeichnet",
        Text: "Multiple offene Wunden, nicht näher bezeichnet",
        Status: "assured",
        IsLeading: false,
        __typename: "ProcessICD",
      },
      {
        _id: 85073,
        Mk: 3,
        Code: "T08.0",
        ActineoText: "Fraktur der Wirbelsäule",
        Text: "Fraktur der Wirbelsäule, Höhe nicht näher bezeichnet: geschlossen",
        Status: "assured",
        IsLeading: false,
        __typename: "ProcessICD",
      },
      {
        _id: 85074,
        Mk: 1,
        Code: "J93.9",
        ActineoText: "Pneumothorax",
        Text: "Pneumothorax, nicht näher bezeichnet",
        Status: "assured",
        IsLeading: false,
        __typename: "ProcessICD",
      },
      {
        _id: 634234,
        Mk: 1,
        Code: "Z96.64",
        ActineoText: "Vorhandensein einer Hüftgelenkprothese",
        Text: "Vorhandensein einer Hüftgelenkprothese",
        Status: "unknown",
        IsLeading: false,
        __typename: "ProcessICD",
      },
      {
        _id: 634235,
        Mk: 1,
        Code: "M87.95",
        ActineoText:
          "Knochennekrose, nicht näher bezeichnet: Beckenregion und Oberschenkel",
        Text: "Knochennekrose, nicht näher bezeichnet: Beckenregion und Oberschenkel",
        Status: "unknown",
        IsLeading: false,
        __typename: "ProcessICD",
      },
      {
        _id: 387916,
        Mk: 3,
        Code: "S32.01",
        ActineoText: null,
        Text: "Fraktur eines Lendenwirbels: L1",
        Status: "unknown",
        IsLeading: false,
        __typename: "ProcessICD",
      },
      {
        _id: 387917,
        Mk: 3,
        Code: "S32.02",
        ActineoText: null,
        Text: "Fraktur eines Lendenwirbels: L2",
        Status: "unknown",
        IsLeading: false,
        __typename: "ProcessICD",
      },
      {
        _id: 610262,
        Mk: 3,
        Code: "S12.24",
        ActineoText: "Fraktur des 6. Halswirbels",
        Text: "Fraktur des 6. Halswirbels",
        Status: "unknown",
        IsLeading: true,
        __typename: "ProcessICD",
      },
      {
        _id: 387909,
        Mk: 3,
        Code: "S72.3",
        ActineoText: null,
        Text: "Fraktur des Femurschaftes",
        Status: "unknown",
        IsLeading: false,
        __typename: "ProcessICD",
      },
      {
        _id: 387908,
        Mk: 4,
        Code: "S71.88",
        ActineoText: null,
        Text: "Weichteilschaden II. Grades bei offener Fraktur oder Luxation der Hüfte und des Oberschenkels",
        Status: "unknown",
        IsLeading: true,
        __typename: "ProcessICD",
      },
      {
        _id: 385884,
        Mk: 1,
        Code: "S71.1",
        ActineoText: null,
        Text: "Offene Wunde des Oberschenkels",
        Status: "unknown",
        IsLeading: false,
        __typename: "ProcessICD",
      },
      {
        _id: 387911,
        Mk: 3,
        Code: "S82.18",
        ActineoText: null,
        Text: "Fraktur des proximalen Endes der Tibia: Sonstige",
        Status: "unknown",
        IsLeading: false,
        __typename: "ProcessICD",
      },
      {
        _id: 387910,
        Mk: 3,
        Code: "S81.88",
        ActineoText: null,
        Text: "Weichteilschaden II. Grades bei offener Fraktur oder Luxation des Unterschenkels",
        Status: "unknown",
        IsLeading: false,
        __typename: "ProcessICD",
      },
      {
        _id: 385887,
        Mk: 1,
        Code: "S81.0",
        ActineoText: null,
        Text: "Offene Wunde des Knies",
        Status: "unknown",
        IsLeading: false,
        __typename: "ProcessICD",
      },
      {
        _id: 385888,
        Mk: 3,
        Code: "S82.38",
        ActineoText: null,
        Text: "Distale Fraktur der Tibia: Sonstige",
        Status: "unknown",
        IsLeading: false,
        __typename: "ProcessICD",
      },
      {
        _id: 385889,
        Mk: 3,
        Code: "S82.5",
        ActineoText: null,
        Text: "Fraktur des Innenknöchels",
        Status: "unknown",
        IsLeading: false,
        __typename: "ProcessICD",
      },
      {
        _id: 387905,
        Mk: 3,
        Code: "S61.87",
        ActineoText: null,
        Text: "Weichteilschaden I. Grades bei offener Fraktur oder Luxation des Handgelenkes und der Hand",
        Status: "unknown",
        IsLeading: false,
        __typename: "ProcessICD",
      },
      {
        _id: 387906,
        Mk: 2,
        Code: "S62.31",
        ActineoText: null,
        Text: "Fraktur eines sonstigen Mittelhandknochens: Basis",
        Status: "unknown",
        IsLeading: false,
        __typename: "ProcessICD",
      },
      {
        _id: 387907,
        Mk: 2,
        Code: "S63.2",
        ActineoText: null,
        Text: "Multiple Luxationen der Finger",
        Status: "unknown",
        IsLeading: false,
        __typename: "ProcessICD",
      },
      {
        _id: 387912,
        Mk: 3,
        Code: "S82.31",
        ActineoText: null,
        Text: "Distale Fraktur der Tibia: Mit Fraktur der Fibula (jeder Teil)",
        Status: "unknown",
        IsLeading: false,
        __typename: "ProcessICD",
      },
      {
        _id: 387913,
        Mk: 3,
        Code: "S22.44",
        ActineoText: null,
        Text: "Rippenserienfraktur: Mit Beteiligung von vier und mehr Rippen",
        Status: "unknown",
        IsLeading: false,
        __typename: "ProcessICD",
      },
      {
        _id: 387914,
        Mk: 3,
        Code: "S22.20",
        ActineoText: null,
        Text: "Fraktur des Sternums: Nicht näher bezeichnet",
        Status: "unknown",
        IsLeading: false,
        __typename: "ProcessICD",
      },
      {
        _id: 387918,
        Mk: 2,
        Code: "D62",
        ActineoText: null,
        Text: "Akute Blutungsanämie",
        Status: "unknown",
        IsLeading: false,
        __typename: "ProcessICD",
      },
    ],
    __typename: "Person",
  },
  Blocks: [
    {
      Name: "PropertyDamageCost",
      IsRounded: false,
      Coefficient: 1,
      Sum: 1390,
      WithSeverancePay: false,
      SeverancePayDuration: 1,
      CapitalisationRate: 0.025,
      DynamisationRate: 1,
      CostSharingAgreement: {
        Active: false,
        Rate: 1,
        Limit: 0,
        Unlimited: false,
        __typename: "CostSharingAgreement",
      },
      __typename: "CalculationBlock",
    },
    {
      Name: "HealingTreatmentCost",
      IsRounded: false,
      Coefficient: 1,
      Sum: 200,
      WithSeverancePay: false,
      SeverancePayDuration: 1,
      CapitalisationRate: 0.025,
      DynamisationRate: 1,
      CostSharingAgreement: {
        Active: false,
        Rate: 1,
        Limit: 0,
        Unlimited: false,
        __typename: "CostSharingAgreement",
      },
      __typename: "CalculationBlock",
    },
    {
      Name: "IncomeCost",
      IsRounded: false,
      Coefficient: 1,
      Sum: 0,
      WithSeverancePay: null,
      SeverancePayDuration: null,
      CapitalisationRate: 0.025,
      DynamisationRate: 1,
      CostSharingAgreement: {
        Active: false,
        Rate: 1,
        Limit: 0,
        Unlimited: false,
        __typename: "CostSharingAgreement",
      },
      __typename: "CalculationBlock",
    },
    {
      Name: "HousekeepingDamageCost",
      IsRounded: false,
      Coefficient: 1,
      Sum: 0,
      WithSeverancePay: false,
      SeverancePayDuration: 1,
      CapitalisationRate: 0.025,
      DynamisationRate: 1,
      CostSharingAgreement: {
        Active: false,
        Rate: 1,
        Limit: 0,
        Unlimited: false,
        __typename: "CostSharingAgreement",
      },
      __typename: "CalculationBlock",
    },
    {
      Name: "IncreasedNeeds",
      IsRounded: false,
      Coefficient: 1,
      Sum: 0,
      WithSeverancePay: false,
      SeverancePayDuration: 1,
      CapitalisationRate: 0.025,
      DynamisationRate: 1,
      CostSharingAgreement: {
        Active: false,
        Rate: 1,
        Limit: 0,
        Unlimited: false,
        __typename: "CostSharingAgreement",
      },
      __typename: "CalculationBlock",
    },
    {
      Name: "DamageCost",
      IsRounded: false,
      Coefficient: 1,
      Sum: 0,
      WithSeverancePay: false,
      SeverancePayDuration: 1,
      CapitalisationRate: 0.025,
      DynamisationRate: 1,
      CostSharingAgreement: {
        Active: false,
        Rate: 1,
        Limit: 0,
        Unlimited: false,
        __typename: "CostSharingAgreement",
      },
      __typename: "CalculationBlock",
    },
    {
      Name: "SurvivorsPensionCost",
      IsRounded: false,
      Coefficient: 1,
      Sum: 0,
      WithSeverancePay: false,
      SeverancePayDuration: 1,
      CapitalisationRate: 0.025,
      DynamisationRate: 1,
      CostSharingAgreement: {
        Active: false,
        Rate: 1,
        Limit: 0,
        Unlimited: false,
        __typename: "CostSharingAgreement",
      },
      __typename: "CalculationBlock",
    },
    {
      Name: "MaintenanceDamageCost",
      IsRounded: false,
      Coefficient: 1,
      Sum: 0,
      WithSeverancePay: false,
      SeverancePayDuration: 1,
      CapitalisationRate: 0.025,
      DynamisationRate: 1,
      CostSharingAgreement: {
        Active: false,
        Rate: 1,
        Limit: 0,
        Unlimited: false,
        __typename: "CostSharingAgreement",
      },
      __typename: "CalculationBlock",
    },
  ],
};

const filtered = data.Person.Icds.filter((diag) => diag.IsLeading);

const newData = { ...data, Person: { ...data.Person, Icds: filtered } };

console.log(newData);

CodePudding user response:

you could do something like this:

// create de reduce over calculations array
const targets = calculations.reduce((reducer,calc)=>{
  // check if Person has any Icds element has IsLeading === true
  const IcdsLeading = calc.Person.Icds.filter((icd) => icd.IsLeading === true);

  if(IcdsLeading.length){
    const p = {
      /**take the relevant Person data do you need here**/
      //someData: calcPerson.....
      IcdsLeading
    };
    reducer.push(p);
  }
  //always return the reducer array
  return reducer;
},[]);

CodePudding user response:

const isLeading = data.calculations.map((cal) => cal.Person.Icds.filter((x) => x.isLeading === true))

Note: In your example there is more than one isLeading that equals true

  • Related