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

未知key名称时如何访问object内部的嵌套数据

问题根因

属性访问层级错误:

  • item是NEWRATES对应standard、ltc字段的数组本身
  • item[i]才是包含动态org名称为key的对象实例
  • 你直接在数组item上访问org对应的属性,不存在该属性所以返回undefined,将item[org]改为item[i][org]即可读取到目标嵌套对象。

额外优化点:Object.keys(item[i]).toString()的写法只适用于对象只有1个key的场景,更稳妥的写法是取数组第一个元素:const org = Object.keys(item[i])[0]。

修正后可读取校验 Physicians/NonPhysicians 的代码

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,
      },
    },
  ],
};

if (NEWRATES) {
  const rateGroups = Object.values(NEWRATES);
  const byProviderType = rateGroups.map(group => {
    return group.map(orgObj => {
      // 取动态org key
      const orgName = Object.keys(orgObj)[0];
      const orgDetail = orgObj[orgName];
      // 校验并读取目标字段
      const hasPhysicians = 'Physicians' in orgDetail;
      const hasNonPhysicians = 'NonPhysicians' in orgDetail;
      console.log(`机构${orgName}校验结果:`, {hasPhysicians, hasNonPhysicians});
      // 可自行扩展校验逻辑,返回需要的结构
      return {
        orgName,
        Physicians: hasPhysicians ? orgDetail.Physicians : null,
        NonPhysicians: hasNonPhysicians ? orgDetail.NonPhysicians : null
      }
    })
  })
  console.log('整理后的数据:', byProviderType);
}

内容的提问来源于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 12:45:04