Search code examples
javascriptobjectnested

How to access a specific array value nested in an object?


I have a JSON file I am pulling via an API, and it is coming back as nested objects with arrays as values.

I am specifically trying to pull, from each of these date properties, the 3rd value from the 'rates' nested object (so .5 from the first, .3374 from the second, etc).

Then I would like to average them and return one number.

{
    "2022-09-28": {
        "first": 14,
        "counts": [
            14,
            7,
            7
        ],
        "rates": [
            1.0,
            0.5,
            0.5
        ]
    },
    "2022-10-01": {
        "first": 572,
        "counts": [
            500,
            210,
            193
        ],
        "rates": [
            0.8741,
            0.3671,
            0.3374
        ]
    },
    "2022-11-01": {
        "first": 873,
        "counts": [
            776,
            327,
            298
        ],
        "rates": [
            0.8889,
            0.3746,
            0.3414
        ]

After json parsing the data, I call the below on it - but I'm a bit stumped as to what to do next here. I've tried a few different ideas (Objects.values for one) but can't seem to get it to return what I need, the multiple layers is breaking my brain a bit.

Here's what I've gotten to so far:

  const nestedRates = (obj) => {
    for (const key in obj) {
      if (typeof obj[key] === 'object') {
        for (const innerKey in obj[key]) {
          if (innerKey == 'rates') {
            
          }
        }
      }
    }
  }

Any ideas? Thank you


Solution

  • Here is the way you can get average

    const dateObj = {
        "2022-09-28": {
            "first": 14,
            "counts": [
                14,
                7,
                7
            ],
            "rates": [
                1.0,
                0.5,
                0.5
            ]
        },
        "2022-10-01": {
            "first": 572,
            "counts": [
                500,
                210,
                193
            ],
            "rates": [
                0.8741,
                0.3671,
                0.3374
            ]
        },
        "2022-11-01": {
            "first": 873,
            "counts": [
                776,
                327,
                298
            ],
            "rates": [
                0.8889,
                0.3746,
                0.3414
            ]
           }
      }
    
      const nestedRates = (obj) => {
      let rates = [];
        for (const key in obj) {
          if (typeof obj[key] === 'object') {
            for (const innerKey in obj[key]) {
              if (innerKey == 'rates' && obj[key] && obj[key].rates[2]) {
               rates.push(obj[key].rates[2])
              }
            }
          }
        }
        return rates;
      }
      const rates = nestedRates(dateObj);
    const sum = rates.reduce((a, b) => a + b, 0);
    const avg = (sum / rates.length) || 0;
    console.log(avg);