在不知道键名的情况下访问对象内部的数据
Accessing the data inside an object when you dont know the name of the key
我有一组数据,其中数据的键是不可预测的。我正在尝试读取嵌套对象,但似乎无法访问它,因此我可以检查下一个键 Physicians
或 NonPhysicians
的值。我尝试使用嵌套值的键来访问它,但它只有 returns 未定义。当我控制台输出 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]);
}
});
你很接近。你需要继续深入一个层次。
// Your Code Now
console.log(item[org]);
// SHOULD BE
console.log(item[i][org]);
进行此更新,您会看到它正常运行。 HERE is a working version 在 stackblitz 上。
我有一组数据,其中数据的键是不可预测的。我正在尝试读取嵌套对象,但似乎无法访问它,因此我可以检查下一个键 Physicians
或 NonPhysicians
的值。我尝试使用嵌套值的键来访问它,但它只有 returns 未定义。当我控制台输出 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]);
}
});
你很接近。你需要继续深入一个层次。
// Your Code Now
console.log(item[org]);
// SHOULD BE
console.log(item[i][org]);
进行此更新,您会看到它正常运行。 HERE is a working version 在 stackblitz 上。