You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JS for循环处理数据返回对象时生成额外嵌套数组问题求助

问题原因

  • Object.values(NEWRATES)会得到[standard数组, ltc数组]两个数组,你用map遍历处理的话,每次遍历都会生成一个独立的{associates, telehealth}对象,最终返回的是长度为2的对象数组,自然出现嵌套结构
  • 每轮遍历只能处理一类数据:处理standard数组时associates数组是空,处理ltc数组时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 associates = []
const telehealth = []

// 把所有机构数据合并成一个数组统一遍历
Object.values(NEWRATES).flat().forEach(orgItem => {
  // 取机构配置对象
  const orgConfig = Object.values(orgItem)[0]
  if (!orgConfig[activeTab]) return
  // 按ltc标识分类推入对应数组
  if (orgConfig.ltc) {
    associates.push(orgConfig[activeTab].associates)
  } else {
    telehealth.push(orgConfig[activeTab].telehealth)
  }
})

// 最终得到你要的结构
const byProviderType = { associates, telehealth }
console.log(byProviderType)

效果验证

运行后输出的byProviderType就是你期望的结构:

{
  associates: [{/* Infinity的Physicians associates配置 */}],
  telehealth: [{/* ORG A的Physicians telehealth配置 */}, {/* ORG B的Physicians telehealth配置 */}]
}

内容的提问来源于stack exchange,提问作者CourtneyJ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 13:15:05