简体   繁体   中英

Find index by breakpoints instead of multiple else if statements

I am trying to generate sizes based on a persons height. For the moment my program works but I would like to find a shorter way to get the size instead of using all these else if statements. I would like to loop through "breakpoints" to find the corresponding index.

This is my original code + what i had in mind.

 const sizes = ['xxs', 'xxs or xs', 'xs', 'xs or s'] // Goes on for all sizes... function generateSize(height) { let size; if (height < 142) { size = sizes[0]; } else if (height >= 142 && height < 148) { size = sizes[1]; } else if (height >= 148 && height < 154) { size = sizes[2]; } else if (height >= 154 && height < 160) { size = sizes[3]; // Goes on for all sizes... } else { size = 'larger...'; } return size; } // Example of what I had in mind. const heightBreakpoints = [142, 148, 154, 160]; function getByBreakpoints(breakpoints, height){ // Part where I am stuck. let index; // Loop through breakpoints... return index; } const sizeIndex = (getByBreakpoints(heightBreakpoints, 158)); const s = sizes[sizeIndex];

I think you could simplify this greatly just by tweaking your starting data structure. What if we had an array of objects that tie together size and its breakpoint:

const sizeMap = [
    { maxHeight: 142, size: 'xxs' },
    { maxHeight: 148, size: 'xxs or xs' },
    { maxHeight: 154, size: 'xs' },
    { maxHeight: 160, size: 'xs or s' },
]

const getSize = height => sizeMap.find(item => height < item.maxHeight).size

console.log(getSize(143))

Array function find returns the first value that satifsies your condition. The precondition for this approach to work is to have your array object's heights in ascending order.

 const sizes = ['xxs', 'xxs or xs', 'xs', 'xs or s', "even another one here"] // Goes on for all sizes... function generateSize(height){ let size; let left = 142; let right = 142; // Initialize the variables for comparison // Left and right comparison based on position of "&" // This is just user-defined for(var i = 0; i < sizes.length; i++){ if(height < right){ size = sizes[i]; return size; } else { // add counter from here // This takes us to the next item in the array i += 1; // add right comparison with intervals of 6 right += 6; if(height >= left && height < right){ size = sizes[i]; return size; } else { // add left comparison with intervals of 6 left += 6; // revert the counter to its initial value i -= 1; } } } } console.log("First: " + generateSize(141)) console.log("Second: " + generateSize(147)) console.log("Third: " + generateSize(153)) console.log("Fourth: " + generateSize(159)) console.log("Last: " + generateSize(161)); // Note this 161, which will return the new last value in the array

This assumes your sizes are at intervals of 6, (which they are) and returns respective values corresponding to the array

if(height<160){
height-=142;
if(height<0){size=sizes[0]}
else{
size=sizes[(hieght)%6]
}
}
else{
size='larger...'
}

check if this works in all cases I am sleepy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM