gpt4 book ai didi

javascript - 如何通过键迭代并添加对象

转载 作者:太空宇宙 更新时间:2023-11-04 15:39:54 25 4
gpt4 key购买 nike

我正在使用 javascript 中的对象。

1.

Units = [
{
"BedDetails": [
{
"Outpatient": 2,
"ICU_Beds": 0,
"NL_Beds": 2,
"Extra": 9,
"Ordinary": 3,
"ADMIN": 6,
"SVC_Areas": 3.2 // It is different kind not type of bed
}
]
},
{
"BedDetails": [
{
"Outpatient": 4,
"ICU_Beds":2,
"Extra": 2,
"Ordinary": 2,
"ADMIN":2,
"SVC_Areas": 2.1 // It is different kind not type of bed
}
]
}
]

这里我需要执行三个操作

  1. 按床位类型汇总
  2. 如果床类型的类型 > 0,则将 SVC_Areas 求和为 SVC_bedType 。
  3. 床类型/SVC_bedtype 的比率。

例如:

  • 第 1 步是总结床型
  • 第 2 步是如果 bedtype > 0,则对 bedtype 下的 svc 求和,如下

    门诊 > 0 => SVC_out Patient = 2

    ICU > 0 => SVC_ICU = 0//此处 ICU = 0,因此执行求和

  • 第 3 步是在第 1 步/第 2 步中获得的(床型总和)/SVC_bedtype 的比率

这是我到目前为止的表现

Units.forEach(function (element) {               
var Beds_Details = element.BedDetails[0];
Sum_Outpatient += Beds_Details.Outpatient;
Sum_ICUBeds += Beds_Details.ICU_Beds;
Sum_Extra += Beds_Details.Extra;
Sum_Ordinary += Beds_Details.Ordinary;
Sum_ADMIN += Beds_Details.ADMIN;

if (Beds_Details.Outpatient > 0) { (SVC_Outpatient += Beds_Details.SVC_Areas).toFixed(2); }
if (Beds_Details.ICU_Beds > 0) { (SVC_ICUBeds += Beds_Details.SVC_Areas).toFixed(2); }
if (Beds_Details.Extra > 0) { (SVC_Extra += Beds_Details.SVC_Areas).toFixed(2); }
if (Beds_Details.Ordinary > 0) { (SVC_Ordinary += Beds_Details.SVC_Areas).toFixed(2); }
if (Beds_Details.ADMIN > 0) { (SVC_ADMIN += Beds_Details.SVC_Areas).toFixed(2); }
});

if (SVC_Outpatient > 0) {
RatioModel_Outpatient = (Sum_Outpatient / SVC_Outpatient ).toFixed(2);
} else {
RatioModel_Outpatient = 0;
};

if (SVC_ICUBeds > 0) {
RatioModel_ICUBeds = (Sum_ICUBeds / SVC_ICUBeds).toFixed(2);
} else {
RatioModel_ICUBeds = 0;
};

if (SVC_Extra > 0) {
RatioModel_EX_TR = (Sum_Extra / SVC_Extra ).toFixed(2);
} else {
RatioModel_Extra = 0;
};

if (SVC_Ordinary > 0) {
RatioModel_Ordinary = (Sum_Ordinary / SVC_Ordinary ).toFixed(2);
} else {
RatioModel_Ordinary = 0;
};

if (SVC_ADMIN > 0) {
RatioModel_ADMIN = (Sum_ADMIN / SVC_ADMIN).toFixed(2);
} else {
RatioModel_ADMIN = 0;
};

虽然代码工作正常,但我需要通过 Bed_details 键循环优化代码。

谁能帮忙解决这两个问题

最佳答案

您根据我们的聊天提出的要求已在此处实现。您将在输出对象中获得输出。

在这里找到工作 fiddle AnswerFiddle

var output = {};
function getPropertySum(propertyName){
var sum = 0;
Units.forEach(function(obj){
var item = obj.BedDetails["0"];
sum = sum + (item[propertyName]||0);
});
return sum;
};
function getPropertySVC(propertyName){
var svc = 0;
Units.forEach(function(obj){
var item = obj.BedDetails["0"];
if(item[propertyName]>0){
svc = svc + (item.SVC_Areas||0);
}
});
return parseFloat(svc.toFixed(2));
};

function getPropertyDetails(propertyName){
var svc = getPropertySVC(propertyName);
var sum = getPropertySum(propertyName);
var ratio = svc === 0 ? 0: sum/svc;
output["Sum_"+ propertyName] = sum;
output["SVC_"+ propertyName] = svc;
output["RatioModel_"+ propertyName] = parseFloat(ratio.toFixed(2));

}

var bedItem = Units[0].BedDetails[0];
for(var property in bedItem){
if(property!=="SVC_Areas"){
getPropertyDetails(property);
}
}
console.log(output);

关于javascript - 如何通过键迭代并添加对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43979623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com