当您不知道键的名称时访问对象内部的数据

Posted

技术标签:

【中文标题】当您不知道键的名称时访问对象内部的数据【英文标题】: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]);
      
    );

【问题讨论】:

Object.values() 这是一个正在运行的堆栈闪电战:stackblitz.com/edit/js-6gbjyk 【参考方案1】:

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

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

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

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

【讨论】:

以上是关于当您不知道键的名称时访问对象内部的数据的主要内容,如果未能解决你的问题,请参考以下文章

oracle同义词

如果我不知道名称,如何访问 javascript 对象的属性?

如何在不知道子类名称的情况下访问 django 中对象的子类?

当您不记得约束名称时如何删除约束[重复]

在不知道对象键的情况下访问对象值

如何访问 Javascript 对象的第一个属性?