1

Below is the JSON response I am getting. From this JSON response, I want to get the value of "fees" based on "detailComponent" with "FULL_FEE" only. But, somehow it gets the last value of "detailComponent" with "SUB_FEE" or others which is not correct.

I am sure not how to make this for loop condition to fix my issue. Can help to guide pls?

let data = {
    "status": "SUCCESS",
    "result": {
        "originData": {
            "detailType": "MSG",
            "origin": [
                {
                    "details": [
                        {
                            "detailComponent": "FULL_FEE",
                            "fees": 13564.00
                        },
                        {
                            "detailComponent": "SUB_FEE",
                            "fees": 8207.60
                        }
                    ]
                }
            ]
        }
    }
}

var getData = data.result.originData.origin[0].details[0].detailComponent;
console.log('getData: ', getData);
WTE
  • 301
  • 1
  • 7

1 Answers1

0

You can convert the array into a dictionary by the key (detailComponent) and the number (fees).

// Create Array
var items = [];

// Loop Through It
data.result.originData.origin[0].details.forEach((el, index) => items[el.detailComponent] = el.fees);

// Test
console.log(items["FULL_FEE"]);
Ctznkane525
  • 7,297
  • 3
  • 16
  • 40