通过断点而不是多个 else if 语句查找索引

我正在尝试根据人的身高生成尺寸。目前我的程序可以工作,但我想找到一种更短的方法来获取大小,而不是使用所有这些else if语句。我想循环通过“断点”来找到相应的索引。


这是我的原始代码+我的想法。


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];


慕慕森
浏览 122回答 3
3回答

跃然一笑

&nbsp;const sizes = ['xxs', 'xxs or xs', 'xs', 'xs or s', "even another one here"]// Goes on for all sizes...function generateSize(height){&nbsp; &nbsp; let size;&nbsp; &nbsp; let left = 142;&nbsp; &nbsp; let right = 142;&nbsp; &nbsp; // Initialize the variables for comparison&nbsp; &nbsp; // Left and right comparison based on position of "&"&nbsp; &nbsp; // This is just user-defined&nbsp; &nbsp; for(var i = 0; i < sizes.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; if(height < right){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size = sizes[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return size;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // add counter from here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // This takes us to the next item in the array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i += 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // add right comparison with intervals of 6&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; right += 6;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(height >= left && height < right){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size = sizes[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return size;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // add left comparison with intervals of 6&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; left += 6;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // revert the counter to its initial value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i -= 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}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这假设您的大小间隔为 6,(它们是)并返回与数组对应的相应值

慕桂英546537

我认为你可以通过调整你的起始数据结构来大大简化这一点。如果我们有一个将大小及其断点联系在一起的对象数组怎么办:const sizeMap = [&nbsp; &nbsp; { maxHeight: 142, size: 'xxs' },&nbsp; &nbsp; { maxHeight: 148, size: 'xxs or xs' },&nbsp; &nbsp; { maxHeight: 154, size: 'xs' },&nbsp; &nbsp; { maxHeight: 160, size: 'xs or s' },]const getSize = height => sizeMap.find(item => height < item.maxHeight).sizeconsole.log(getSize(143))数组函数find返回满足您条件的第一个值。这种方法工作的先决条件是让您的数组对象的高度按升序排列。

牧羊人nacy

if(height<160){height-=142;if(height<0){size=sizes[0]}else{size=sizes[(hieght)%6]}}else{size='larger...'}检查这是否适用于所有情况我困了
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript