【问题标题】:Accessing the data inside an object when you dont know the name of the key当您不知道键的名称时访问对象内部的数据
【发布时间】:2021-12-10 14:18:08
【问题描述】:

我有一组数据,其中数据的关键是不可预测的。我正在尝试读取嵌套对象,但似乎无法访问它,因此我可以检查下一个键 PhysiciansNonPhysicians 的值。我尝试使用嵌套值的键来访问它,但它只返回未定义。当我安慰 item 时,我得到了预期值,当我安慰 org 时,我得到了对象上的键,所以我不确定这里出了什么问题。

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

const sortData = Object.values(NEWRATES);
  const NEWfiltered = !!NEWRATES && sortData;
  const byProviderType =
    !!NEWfiltered &&
    NEWfiltered.map((item, idx) => {
      for (let i = 0; i < item.length; i++) {
        let list = [];
        let org = Object.keys(item[i]).toString();

        console.log(item[org]);
      }
    });

【问题讨论】:

标签: javascript for-loop object nested-object


【解决方案1】:

你很接近。你需要继续更深一层。

// Your Code Now
console.log(item[org]);

// SHOULD BE
console.log(item[i][org]);

进行此更新,您将看到它正常工作。 HERE is a working version 在 stackblitz 上。

【讨论】:

    猜你喜欢
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    相关资源
    最近更新 更多