Home > Back-end >  How can I accurately return nested values from JSON for comparison and then complete the comparison?
How can I accurately return nested values from JSON for comparison and then complete the comparison?

Time:10-19

I have asked a similar question before but I've not been able to expand on this and I haven't found the exact answer that would teach me how to do this. My JSON file will have the same structure in terms of the element names inside each nested object but the values will vary. In plain English, I want to start at the bottom of the file and return true if the following conditions occur and stop searching if true: AppStatus is one of these ["Approved", "Auto Approved", "Return"] and the nested keys of "Name": "Payments", "Value": "X" and "Name": "Term", "Value": "Y" have values that don't match. I'm providing a snippet of the JSON so you can see more easily. The AppStatus is 2nd level and the Name, Value are 3rd level. In this example, it should return true based on the 2nd set of data in DataElements.

{
    "DecisionHistory": [
        {
            "Id": "273601",
            "Number": "1",
            "CreateDate": "2022-10-13 15:31:18.683",
            "AppStatus": "Approved",
            "DataElements": [
                {
                    "Id": "213922",
                    "Name": "Payments",
                    "Value": "72",
                    "GroupId": "12",
                    "CustomLabel": null
                },
                {
                    "Id": "990",
                    "Name": "Decisioned By",
                    "Value": "ASDF",
                    "GroupId": "3",
                    "CustomLabel": null
                },
                {
                    "Id": "215337",
                    "Name": "Term",
                    "Value": "75",
                    "GroupId": "13",
                    "CustomLabel": null
                }
            ]
        },
        {
            "Id": "273601",
            "Number": "2",
            "CreateDate": "2022-10-13 15:31:18.683",
            "AppStatus": "Approved",
            "DataElements": [
                {
                    "Id": "213922",
                    "Name": "Payments",
                    "Value": "72",
                    "GroupId": "12",
                    "CustomLabel": null
                },
                {
                    "Id": "990",
                    "Name": "Decisioned By",
                    "Value": "ASDF",
                    "GroupId": "3",
                    "CustomLabel": null
                },
                {
                    "Id": "215337",
                    "Name": "Term",
                    "Value": "75",
                    "GroupId": "13",
                    "CustomLabel": null
                }
            ]
        },
        {
            "Id": "273601",
            "Number": "3",
            "CreateDate": "2022-10-13 15:31:18.683",
            "AppStatus": "Approved",
            "DataElements": [
                {
                    "Id": "213922",
                    "Name": "Payments",
                    "Value": "75",
                    "GroupId": "12",
                    "CustomLabel": null
                },
                {
                    "Id": "990",
                    "Name": "Decisioned By",
                    "Value": "ASDF",
                    "GroupId": "3",
                    "CustomLabel": null
                },
                {
                    "Id": "215337",
                    "Name": "Term",
                    "Value": "75",
                    "GroupId": "13",
                    "CustomLabel": null
                }
            ]
        }
    ]
}

I've tried using 2 ugly nested for loops without success. I would prefer not to use for loops if I don't have to. See below:

function main(jsonFile) {
  const history = JSON.parse(jsonFile).DecisionHistory;
  const appStatus = ["Approved", "Auto Approved", "Return"];
  const termMonths = "TermMonths";
  const numberOfPayments = "Number of Payments";
  let termMonthsVal = 0;
  let numberofPaymentsVal = 0;

  for (let a = history.length - 1; a >= 0; a--) {
    if (appStatus.includes(history[a].AppStatus)) {
      var dataElementsA = history[a].DataElements;
      for (let b = (dataElementsA.length) - 1; b >= 0; b--) {
        if (dataElementsA[b].Name == termMonths) {
          termMonthsVal = new Number((dataElementsA[b].Value));
          break;
        }
      }
    }
  }
  for (let a = history.length - 1; a >= 0; a--) {
    if (appStatus.includes(history[a].AppStatus)) {
      var dataElementsB = history[a].DataElements;
      for (let b = (dataElementsB.length) - 1; b >= 0; b--) {
        if (dataElementsB[b].Name == numberOfPayments) {
          numberofPaymentsVal = new Number((dataElementsB[b].Value));
          break;
        }
      }
    }
  }
  return (termMonthsVal != numberofPaymentsVal);
}


let jsonFile = `{
  "DecisionHistory": [{
      "Id": "273601",
      "Number": "1",
      "CreateDate": "2022-10-13 15:31:18.683",
      "AppStatus": "Approved",
      "DataElements": [{
          "Id": "213922",
          "Name": "Payments",
          "Value": "72",
          "GroupId": "12",
          "CustomLabel": null
        },
        {
          "Id": "990",
          "Name": "Decisioned By",
          "Value": "ASDF",
          "GroupId": "3",
          "CustomLabel": null
        },
        {
          "Id": "215337",
          "Name": "Term",
          "Value": "75",
          "GroupId": "13",
          "CustomLabel": null
        }
      ]
    },
    {
      "Id": "273601",
      "Number": "2",
      "CreateDate": "2022-10-13 15:31:18.683",
      "AppStatus": "Approved",
      "DataElements": [{
          "Id": "213922",
          "Name": "Payments",
          "Value": "72",
          "GroupId": "12",
          "CustomLabel": null
        },
        {
          "Id": "990",
          "Name": "Decisioned By",
          "Value": "ASDF",
          "GroupId": "3",
          "CustomLabel": null
        },
        {
          "Id": "215337",
          "Name": "Term",
          "Value": "75",
          "GroupId": "13",
          "CustomLabel": null
        }
      ]
    },
    {
      "Id": "273601",
      "Number": "3",
      "CreateDate": "2022-10-13 15:31:18.683",
      "AppStatus": "Approved",
      "DataElements": [{
          "Id": "213922",
          "Name": "Payments",
          "Value": "75",
          "GroupId": "12",
          "CustomLabel": null
        },
        {
          "Id": "990",
          "Name": "Decisioned By",
          "Value": "ASDF",
          "GroupId": "3",
          "CustomLabel": null
        },
        {
          "Id": "215337",
          "Name": "Term",
          "Value": "75",
          "GroupId": "13",
          "CustomLabel": null
        }
      ]
    }
  ]
}`;

console.log(main(jsonFile));

//var result = main("_inJson");
//module.exports = main;

As a side note, the jsonFile in the main function is read in from a resource folder in my VS Code and I'm exporting main to an app.js so I can run assertions against it. So the JSON posted here is actually in a separate file but this is the exact structure of the data.

What am I missing? .filter? .map? .reduce? Differnt breaks?

CodePudding user response:

Here's a "one-liner" depending on how long you want your lines to be ;)

const x = {
    "DecisionHistory": [
        {
            "AppStatus": "Approved",
            "DataElements": [
                {
                    "Name": "Payments",
                    "Value": "72",
                },
                {
                    "Name": "Decisioned By",
                    "Value": "ASDF",
                },
                {
                    "Name": "Term",
                    "Value": "75",
                }
            ]
        },
        {
            "AppStatus": "Approved",
            "DataElements": [
                {
                    "Name": "Payments",
                    "Value": "72",
                },
                {
                    "Name": "Decisioned By",
                    "Value": "ASDF",
                },
                {
                    "Name": "Term",
                    "Value": "75",
                }
            ]
        },
        {
            "AppStatus": "Approved",
            "DataElements": [
                {
                    "Name": "Payments",
                    "Value": "75",
                },
                {
                    "Name": "Decisioned By",
                    "Value": "ASDF",
                },
                {
                    "Name": "Term",
                    "Value": "75",
                }
            ]
        }
    ]
};

const bar = (f) =>
    f.DecisionHistory.reverse().some((decision) => ["Approved", "Auto Approved", "Return"].includes(decision.AppStatus) && decision.DataElements.find((el) => el.Name === 'Payments')?.Value !== decision.DataElements.find((el) => el.Name === 'Term')?.Value);

console.log(bar(x));
  • Related