-1

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

Phil
  • 51
  • 5
  • 1
    What happens when you try this? Is it not what you expect to see? Please post the results of what you tried and explain how they are different from what you expected to see. – Matt Morgan Mar 27 '23 at 11:51
  • 1
    `for(const key in obj) { console.log(obj[key].rates[2]); }` – Guy Incognito Mar 27 '23 at 11:52
  • `const vals = Object.values(obj); const res = vals.reduce((a, {rates}) => a + rates[2],0)/vals.length` – cmgchess Mar 27 '23 at 11:59

1 Answers1

1

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);
Srushti Shah
  • 810
  • 3
  • 17