【发布时间】:2021-12-10 14:18:08
【问题描述】:
我有一组数据,其中数据的关键是不可预测的。我正在尝试读取嵌套对象,但似乎无法访问它,因此我可以检查下一个键 Physicians 或 NonPhysicians 的值。我尝试使用嵌套值的键来访问它,但它只返回未定义。当我安慰 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]);
}
});
【问题讨论】:
-
这是一个正在运行的堆栈闪电战:stackblitz.com/edit/js-6gbjyk
标签: javascript for-loop object nested-object