Home > Mobile >  for loop creating extra arrays in return
for loop creating extra arrays in return

Time:10-26

I am trying to push data into an array and return both arrays in an object. The shape of this data is really giving me a hard time... what I am trying to return is something like this:

{
associates: [{...},{...}],
 telehealth: [{...},{...}] 
}

But instead I get nested arrays returned with an extra empty array for each type. Heres a working example any advice on this would be greatly appreciated The shape of this data is really giving me a hard time... :

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

  const sortData = Object.values(NEWRATES);
  const NEWfiltered = !!NEWRATES && sortData;

  const byProviderType =
    !!NEWfiltered &&
    NEWfiltered.map((item, idx) => {
      const associatesList = [];
      const telehealthList = [];
      for (let i = 0; i < item.length; i  ) {
        let orgKeys = Object.keys(item[i]).toString();
        let org = item[i][orgKeys];
        // if the object org.Physicians and the type is telehealth push into the array
        if (!org.ltc && org[activeTab]) {
          telehealthList.push(org[activeTab].telehealth);
        } else if (!!org.ltc && org[activeTab]) {
          associatesList.push(org[activeTab].associates);
        }
      }
      return {telehealth: telehealthList, associates: associatesList};
    });

  console.log(byProviderType, "NEWRATES:");
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Map return array composed of items returned at every itteration. That is why you have array of arrays. Just move both lists at above scope and use forEach.

const sortData = Object.values(NEWRATES);
const NEWfiltered = !!NEWRATES && sortData;
const associatesList = [];
const telehealthList = [];
!!NEWfiltered &&
    NEWfiltered.forEach((item, idx) => {
        for (let i = 0; i < item.length; i  ) {
            let orgKeys = Object.keys(item[i]).toString();
            let org = item[i][orgKeys];
            // if the object org.Physicians and the type is telehealth push into the array
            if (!org.ltc && org[activeTab]) {
                telehealthList.push(org[activeTab].telehealth);
            } else if (!!org.ltc && org[activeTab]) {
                associatesList.push(org[activeTab].associates);
            }
        }
    });
console.log({ associatesList, telehealthList }, "NEWRATES:");
```javascript

  • Related