If you make an array indexed by ageGroup
, whose values are objects containing the hemoglobineValue
and its associated lower threshold for that age group, you can iterate through the object's entries to .find
the first matching entry:
const hValues = [
// Age group 0:
{
AST: 6.9,
AS: 7.9,
AM: 11.4,
AL: 13.4,
HN: 19.8,
PL: 100
},
// Age group 1:
{
AST: 6.9,
AS: 7.9,
AM: 9.9,
AL: 10.9,
HN: 13,
PL: 100
}
// etc
]
function hemoglobine() {
hemoglobineInput.addEventListener('input', () => {
const input = Number(document.querySelector('#hemoglobina').value);
const entry = Object.entries(hValues[ageGroup])
.find(([, threshold]) => input <= threshold);
const hemoglobineContainerValue = entry[0];
});
}
You could also use an array of arrays instead of an array of objects in hValues
to make things more concise, and have a separate array containing the hemoglobineValue
strings, but IMO that data structure would be a bit harder to understand at a glance.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…