Home > Mobile >  Accessing the data inside an object when you dont know the name of the key
Accessing the data inside an object when you dont know the name of the key

Time:10-26

I have a set of data where the key on the data is not predictable. I am trying to read the nested object but I cant seem to access it so I can check the value of the next key Physicians or NonPhysicians. I tried using the key of the nested value to access it but it only returns undefined. When I console out item i get the expected values and when I console out org I get the keys on the objects so im not sure whats going wrong here.

const NEWRATES = {
  standard: [
    {
      "ORG A": {
        Physicians: {
          telehealth: {
            weekdayEncounters: 15,
            weeknightEncounters: 16.25,
            weekendDayEncounters: 16.25,
            weekendNightEncounters: 17.25,
            holidayEncounters: 17.25,
            stipend: 0,
          },
        },
        NonPhysicians: {
          telehealth: {
            orgName: "Standard",
            weekdayEncounters: 15,
            weeknightEncounters: 16.25,
            weekendDayEncounters: 16.25,
            weekendNightEncounters: 17.25,
            holidayEncounters: 17.25,
            stipend: 0,
          },
        },
        date: "07-2021",
        orgName: "some org",
        ltc: false,
      },
    },
    {
      "ORG B": {
        Physicians: {
          telehealth: {
            weekdayEncounters: 15,
            weeknightEncounters: 16.25,
            weekendDayEncounters: 16.25,
            weekendNightEncounters: 17.25,
            holidayEncounters: 17.25,
            stipend: 0,
          },
        },
        NonPhysicians: {
          telehealth: {
            orgName: "Standard",
            weekdayEncounters: 15,
            weeknightEncounters: 16.25,
            weekendDayEncounters: 16.25,
            weekendNightEncounters: 17.25,
            holidayEncounters: 17.25,
            stipend: 0,
          },
        },
        date: "07-2021",
        orgName: "some org",
        ltc: false,
      },
    },
  ],
  ltc: [
    {
      Infinity: {
        Physicians: {
          associates: {
            roundingHours: 10,
            onCallHours: 10,
            weekdayEncounters: 16,
            weeknightEncounters: 17.25,
            weekendDayEncounters: 18.25,
            weekendNightEncounters: 19.25,
            holidayEncounters: 20.25,
            stipend: 0,
          },
        },
        NonPhysicians: {
          associates: {
            roundingHours: 0,
            onCallHours: 0,
            weekdayEncounters: 15,
            weeknightEncounters: 16.25,
            weekendDayEncounters: 16.25,
            weekendNightEncounters: 17.25,
            holidayEncounters: 17.25,
            stipend: 0,
          },
        },
        date: "07-2021",
        orgName: "some org",
        ltc: true,
      },
    },
  ],
};

const sortData = Object.values(NEWRATES);
  const NEWfiltered = !!NEWRATES && sortData;
  const byProviderType =
    !!NEWfiltered &&
    NEWfiltered.map((item, idx) => {
      for (let i = 0; i < item.length; i  ) {
        let list = [];
        let org = Object.keys(item[i]).toString();

        console.log(item[org]);
      }
    });
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You are close. You need to keep going one level deeper.

// Your Code Now
console.log(item[org]);

// SHOULD BE
console.log(item[i][org]);

Make this update, and you will see it working. HERE is a working version on stackblitz.

  • Related