【问题标题】:If statement where value equals one on many?如果语句的值等于一对多?
【发布时间】:2014-09-05 14:14:48
【问题描述】:

所以我正在编写一个时区插件,我需要简化我的 if 语句,因为我想避免重复。我目前正在编写它以按州更改时间(是的,我知道有些州可以有多个时区。我稍后会完善它,可能会按县。)

我们可以将所有可能的值存储在一个变量中,而不是说值是否等于这个或这个或这个或这个等?下面,我有一个太平洋州的数组,我如何判断状态值是否等于其中之一?

jQuery

function GetClientTime() {
    // current time
    var dt = new Date();
    // pacific time
    var pacifictime = dt.getHours() - 2 + ":" + (dt.getMinutes() < 10 ? '0' : '') + dt.getMinutes();
    // mountain time
    var mountaintime = dt.getHours() - 1 + ":" + (dt.getMinutes() < 10 ? '0' : '') + dt.getMinutes();
    // central time
    var centraltime = dt.getHours() + ":" + (dt.getMinutes() < 10 ? '0' : '') + dt.getMinutes();
    // eastern time
    var easterntime = dt.getHours() + 1 + ":" + (dt.getMinutes() < 10 ? '0' : '') + dt.getMinutes();

    // get am/pm
    var hours = new Date().getHours();
    var ampm = (hours >= 12) ? "PM" : "AM";

    var pacificstates = [
        "WA", "OR", "CA", "NV"
    ]

    if ($('#state').val() == pacificstates) {
        $('#currenttime').show().val(pacifictime + " " + ampm);
    } else if ($('#state').val() == 'CO') {
        $('#currenttime').show().val(mountaintime + " " + ampm);
    } else if ($('#state').val() == 'TX' || $('#state').val() == 'LA') {
        $('#currenttime').show().val(centraltime + " " + ampm);
    } else if ($('#state').val() == 'NY' || $('#state').val() == 'NJ') {
        $('#currenttime').show().val(easterntime + " " + ampm);
    } else {
        $('#currenttime').hide();
    }
}

http://jsfiddle.net/q98sLy5c/1/

【问题讨论】:

标签: javascript jquery variables if-statement time


【解决方案1】:

您可以像这样在数组上使用 indexOf() 方法:

if (pacificstates.indexOf($('#state').val()) !== -1)

如果该调用返回 -1,则表示该元素不包含在该数组中。

查看这里的工作示例http://jsfiddle.net/q98sLy5c/2/

【讨论】:

    【解决方案2】:

    使用switch statement

    switch 语句对表达式求值,将表达式的值与case 子句匹配,并执行与该情况相关的语句。

    var result = false;
    
    swtich ( $('#state').val() ) {
        /* If the value is "CO"... */
        case 'CO':
            result = pacifictime + " " + ampm;
            break;
        /* If the value is "TX" or "LA"... */
        case 'TX':
        case 'LA':
            result = entraltime + " " + ampm;
            break;
        default:
            break;
    }
    
    /* If the result was set, display the result. */
    if (result)
        $('#currenttime').show().val( result );
    /* Otherwise, hide it. */
    else
        $('#currenttime').hide();
    

    【讨论】:

    • 好的,我可以将 case 'TX' 设为包含所有可能的中心状态的变量吗?
    • @triplethreat77 您可以使用数组并使用indexOf 检查您的值是否存在于该数组中。
    • 你能举个例子吗?我有一个小提琴设置。
    【解决方案3】:

    如果您尝试使用对象从状态切换到时区,这可能是您最省时的方法。

    var stateToTimeZone = {};
    
    stateToTimeZone['TX'] = entraltime + " " + ampm;
    stateToTimeZone['LA'] = entraltime + " " + ampm;
    stateToTimeZone['CO'] = pacifictime + " " + ampm;
     . . .
    var result = stateToTimeZone[$('#state').val()];
    if (result)
        $('#currenttime').show().val( result );
    /* Otherwise, hide it. */
    else
        $('#currenttime').hide();
    

    基本上,您是在用更快的时间换取更多的内存使用。但我认为这是你在这种情况下想要做的。它也可以很容易地扩展到县,只需使用像“WA_King”这样的键,因为相同的县名可能出现在不同的州。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-19
      • 1970-01-01
      • 1970-01-01
      • 2019-02-12
      • 2020-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多