If语句vs Switch语句

时间:2020-09-18 12:42:00

标签: javascript if-statement switch-statement

我已经在代码中尝试了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;
     }

2 个答案:

答案 0 :(得分:2)

您使用了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];
}

答案 1 :(得分:1)

在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 ==