使用多个参数创建检查以产生三个不同的结果

Creating a check using multiple arguments to produce three different results

本文关键字:三个 结果 参数 创建 检查      更新时间:2023-09-26

我需要找到最简洁的方法来根据两个参数返回三个值中的一个。我认为switch语句是最简洁的,但请建议其他选项。

我有两个参数,typeaction

如果是action == 'close',那么返回的结果应该总是'Hide'

如果action == open,则需要更多检查

如果action == 'open' && type == 'flight'返回show more flights

如果action == 'open' && type == 'protection'返回show protections

在不涉及多个嵌套的if语句的情况下,最简洁的方法是什么?这整个语句已经在另一个由三进制调用的if语句中,所以我真的不想再添加更多。

这是我目前的解决方案,有没有办法让它变轻?

createShowLabels: function(testEnabled,action,type){
                if (testEnabled){
                    if(action === 'open'){
                        switch(type) {
                            case 'flight':
                                return 'Show flight times';
                            case 'protection':
                                return 'What''s this?';
                        }
                    } else {
                        return 'Hide';
                    }
                } else {
                    // Defaults
                    if (action === 'open'){
                        return 'Show more';
                    } else {
                        return 'Show less';
                    }
                }
            },

首先,让我们弄清楚:"简洁"不是"好的"、"可读的"或"可维护的"的同义词。有时候,简洁的代码是好的、可读的、可维护的;有时,它不是。

鉴于您在注释中保证是列出的值,有几种方法:

在我自己的代码中,我可能会使用类似于FDavidov的答案,但格式不同:

if (action == 'close') {
    result = 'Hide';
} else if (type == 'flight') {
    result = 'show more flights';
} else {
    result = 'show protections';
}

但你要求最简洁的方式。条件操作符很简洁,但并不一定非常容易调试:

result = action == 'close' ? 'Hide' : type == 'flight' ? 'show more flights' : 'show protections';

的例子:

function test(action, type) {
  return action == 'close' ? 'Hide' : type == 'flight' ? 'show more flights' : 'show protections';
}
console.log(test('close', 'flight'));
console.log(test('close', 'protections'));
console.log(test('open', 'flight'));
console.log(test('open', 'protections'));


这些都不那么简洁,但更可配置:

可以使用查找对象:

var lookup = {
    'close|flight': 'Hide',
    'close|protections': 'Hide',
    'open|flight': 'show more flights',
    'open|protections': 'show protections'
};
result = lookup[action + '|' + type];

的例子:

var lookup = {
    'close|flight': 'Hide',
    'close|protections': 'Hide',
    'open|flight': 'show more flights',
    'open|protections': 'show protections'
};
function test(action, type) {
    return lookup[action + '|' + type];
}
console.log(test('close', 'flight'));
console.log(test('close', 'protections'));
console.log(test('open', 'flight'));
console.log(test('open', 'protections'));

或者像你说的,一个开关:

switch (action + '|' + type) {
    case 'close|flight':      result = 'Hide'; break;
    case 'close|protections': result = 'Hide'; break;
    case 'open|flight':       result = 'show more flights'; break;
    case 'open|protections':   result = 'show protections'; break;
};

的例子:

function test(action, type) {
    var result;
    switch (action + '|' + type) {
        case 'close|flight':      result = 'Hide'; break;
        case 'close|protections': result = 'Hide'; break;
        case 'open|flight':       result = 'show more flights'; break;
        case 'open|protections':   result = 'show protections'; break;
    };
    return result;
}
console.log(test('close', 'flight'));
console.log(test('close', 'protections'));
console.log(test('open', 'flight'));
console.log(test('open', 'protections'));

如果您正在寻找速记短语,这不是您要找的。这是一种非常简单且可读的方式来编写你想要的内容:

if (action == 'close') {
   return 'Hide';
}
else if (action = 'open') {
    if      (type = 'flight'    ) {return 'show more flights'}
    else if (type = 'protection') {return 'show protections' }
}