0

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:");

1
  • Please show us your attempted code for this, instead of a working example Commented Oct 25, 2021 at 8:08

1 Answer 1

1

Map return array composed of items returned at every iteration. 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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.