我正在嘗試將資料推送到一個陣列中并在一個物件中回傳兩個陣列。這個資料的形狀真的讓我很難過......我想要回傳的是這樣的:
{
associates: [{...},{...}],
telehealth: [{...},{...}]
}
但相反,我得到嵌套陣列,每個型別回傳一個額外的空陣列。這是一個作業示例,將不勝感激此資料的形狀真的讓我很難過......:
const activeTab = "Physicians"
const NEWRATES = {
standard: [
{
"ORG A": {
Physicians: {
telehealth: {
orgName: "ORG A",
weekdayEncounters: 15,
weeknightEncounters: 16.25,
weekendDayEncounters: 16.25,
weekendNightEncounters: 97.25,
holidayEncounters: 17.25,
stipend: 0,
},
},
NonPhysicians: {
telehealth: {
orgName: "ORG A",
weekdayEncounters: 15,
weeknightEncounters: 16.25,
weekendDayEncounters: 16.25,
weekendNightEncounters: 17.25,
holidayEncounters: 17.25,
stipend: 0,
},
},
date: "07-2021",
orgName: "org A",
ltc: false,
},
},
{
"ORG B": {
Physicians: {
telehealth: {
orgName: "ORG B",
weekdayEncounters: 15,
weeknightEncounters: 16.25,
weekendDayEncounters: 22.25,
weekendNightEncounters: 17.25,
holidayEncounters: 17.25,
stipend: 0,
},
},
NonPhysicians: {
telehealth: {
orgName: "ORG B",
weekdayEncounters: 15,
weeknightEncounters: 66.25,
weekendDayEncounters: 16.25,
weekendNightEncounters: 17.25,
holidayEncounters: 17.25,
stipend: 0,
},
},
date: "07-2021",
orgName: "orgB",
ltc: false,
},
},
],
ltc: [
{
Infinity: {
Physicians: {
associates: {
orgName: "Infinity",
roundingHours: 10,
onCallHours: 10,
weekdayEncounters: 16,
weeknightEncounters: 27.25,
weekendDayEncounters: 18.25,
weekendNightEncounters: 19.25,
holidayEncounters: 20.25,
stipend: 0,
},
},
NonPhysicians: {
associates: {
orgName: "Infinity",
roundingHours: 0,
onCallHours: 0,
weekdayEncounters: 15,
weeknightEncounters: 16.25,
weekendDayEncounters: 16.25,
weekendNightEncounters: 17.25,
holidayEncounters: 17.25,
stipend: 0,
},
},
date: "07-2021",
orgName: "infinity",
ltc: true,
},
},
],
};
const sortData = Object.values(NEWRATES);
const NEWfiltered = !!NEWRATES && sortData;
const byProviderType =
!!NEWfiltered &&
NEWfiltered.map((item, idx) => {
const associatesList = [];
const telehealthList = [];
for (let i = 0; i < item.length; i ) {
let orgKeys = Object.keys(item[i]).toString();
let org = item[i][orgKeys];
// if the object org.Physicians and the type is telehealth push into the array
if (!org.ltc && org[activeTab]) {
telehealthList.push(org[activeTab].telehealth);
} else if (!!org.ltc && org[activeTab]) {
associatesList.push(org[activeTab].associates);
}
}
return {telehealth: telehealthList, associates: associatesList};
});
console.log(byProviderType, "NEWRATES:");
uj5u.com熱心網友回復:
Map由每次迭代回傳的專案組成的回傳陣列。這就是為什么你有陣列陣列的原因。只需在上述范圍內移動兩個串列并使用forEach.
const sortData = Object.values(NEWRATES);
const NEWfiltered = !!NEWRATES && sortData;
const associatesList = [];
const telehealthList = [];
!!NEWfiltered &&
NEWfiltered.forEach((item, idx) => {
for (let i = 0; i < item.length; i ) {
let orgKeys = Object.keys(item[i]).toString();
let org = item[i][orgKeys];
// if the object org.Physicians and the type is telehealth push into the array
if (!org.ltc && org[activeTab]) {
telehealthList.push(org[activeTab].telehealth);
} else if (!!org.ltc && org[activeTab]) {
associatesList.push(org[activeTab].associates);
}
}
});
console.log({ associatesList, telehealthList }, "NEWRATES:");
```javascript
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/336681.html
標籤:javascript 数组 循环 目的
