【问题标题】:for loop creating extra arrays in returnfor 循环创建额外的数组作为回报
【发布时间】:2021-12-10 17:40:26
【问题描述】:

我正在尝试将数据推送到一个数组中并在一个对象中返回两个数组。这些数据的形状真的让我很难过......我试图返回的是这样的:

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

但相反,我得到的嵌套数组为每种类型返回了一个额外的空数组。这是一个工作示例,任何对此的建议将不胜感激。这些数据的形状真的让我很难过......:

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

【问题讨论】:

  • 请向我们展示您为此尝试的代码,而不是工作示例

标签: javascript arrays for-loop object


【解决方案1】:

Map 返回数组,由每次迭代返回的项目组成。 这就是为什么你有数组的原因。

只需在上述范围内移动两个列表并使用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

【讨论】:

    猜你喜欢
    • 2013-07-23
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多