微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

If语句vs Switch语句

如何解决If语句vs Switch语句

我已经在代码中尝试了switch和if语句。开关看起来更好imo。但这对我不起作用。 if语句对您有效。你看到问题了吗?我觉得他们在做同样的事情。 (即过滤对象列表)

let sort = {};


//THE IF STATEMENT
if (query.order == 'name_asc') {
        sort = {name : 1 }
     }
     if (query.order == 'name_desc') {
         sort = { name : -1 }
     }
     if ( query.order == 'lowprice') {
         sort = { price : 1 }
     }
     if ( query.order == 'oldest' ) {
         sort = { year : 1 }
     }
     if ( query.order == 'newest' ) {
         sort = { year : -1 }
     }

// THE SWITCH STATEMENT

     switch (query.order) {
        case query.order == 'name_asc':
            sort = { name : 1 }
            console.log(1)
            break;

        case query.order == 'name_desc':
            sort = { name : -1 }
            console.log(2)
            break;

        case query.order == 'lowprice':
            sort = { price : 1 }
            console.log(3)
            break;

        case query.order == 'oldest':
            sort = { year : 1 }
            console.log(4)
            break;
     
        case query.order == 'newest':
            sort = { price : -1 }
            console.log(5)
            break;
        default: console.log(6)
        break;
     }

解决方法

您使用了query.order == 'name_asc等错误的switch语句。计算为布尔值。因此,switch语句尝试将字符串与布尔值进行比较,从而导致从不输入case语句。

正确的用法是

switch (query.order) {
        case 'name_asc':
            sort = { name : 1 }
            console.log(1)
            break;

        case 'name_desc':
            sort = { name : -1 }
            console.log(2)
            break;

        case 'lowprice':
            sort = { price : 1 }
            console.log(3)
            break;

        case 'oldest':
            sort = { year : 1 }
            console.log(4)
            break;
     
        case 'newest':
            sort = { year : -1 }
            console.log(5)
            break;
        default: console.log(6)
        break;
     }

但是,我建议使用对象将条目值映射到结果。当然,这取决于您。

const NAME_ASC = 'name_asc';
const NAME_DESC = 'name_desc';
const PRICE_LOW = 'lowprice';
const PRICE_HIGH = 'highprice';
const CREATION_ASC = 'oldest';
const CREATION_DESC = 'newest';

const sortOrderMapping = {
   [NAME_ASC]: {name: 1},[NAME_DESC]: {name: -1},// fill in all the cases
}


function getSortOrderObject(orderEnum){
   // this returns undefined if orderEnum is not a valid value
   // maybe you want to return a default value,throw an error,or utilize Option monads
   return sortOrderMapping[orderEnum];
}
,

在JavaScript中,switch语句的语法为:

Exception

因此,您应该从n = 1000 # Example number x = None while True: try: x = int(input(f"X (1 to {n}) :")) except ValueError: print(f"{x} is not a number,please enter a number") else: if 1 <= x <= n: # Inclusive of 1 and n,edit the comparisons if needed break else: print(f"{x} is not within range,please enter a valid number") 中删除switch(expression) { case x: // code block break; case y: // code block break; default: // code block } ,例如

query.order ==

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。