[英]How to shorten JavaScript if/else statements?
I have a function based on a lot of if/else statements. 我有一个基于许多if / else语句的函数。 The function works there is no problem with that but I wonder is there a shorter way to rewrite this? 该功能可以正常工作,但我想知道是否有更短的方法来重写它?
month = get_month(field.value); //this is inside another function
function get_month(m){ if(m==='January'){return'01';} else if(m==='Febuary'){return'02';} else if(m==='March'){return'03';} else if(m==='April'){return'04';} else if(m==='May'){return'05';} else if(m==='June'){return'06';} else if(m==='July'){return'07';} else if(m==='August'){return'08';} else if(m==='September'){return'09';} else if(m==='October'){return'10';} else if(m==='November'){return'11';} else if(m==='December'){return'12';}}
I have seen and used this before but I don't know if would work on this scale: 我之前已经看过并使用过它,但是我不知道是否可以在这样的规模上工作:
var x = y !== undefined ? y : 1;
An object would fit this case rather well. 一个对象很适合这种情况。
function getMonth(m) {
var months = {
January: "01",
February: "02",
March: "03",
April: "04",
May: "05",
June: "06",
July: "07",
August: "08",
September: "09",
October: "10",
November: "11",
December: "12"
}
return months[m];
}
You can see this in action on this jsFiddle demo . 您可以在此jsFiddle演示中看到这一点。
You could do something like 你可以做类似的事情
var monthNumbers = {
"January": "01",
"February": "02",
//...
"December": "12",
};
var getMonth = function(m) {
return monthNumbers[m];
};
Better yet, if you're dealing with dates/times in JavaScript, I highly recommend moment.js 更好的是,如果您要使用JavaScript处理日期/时间,我强烈建议您使用moment.js
Use key array: 使用键数组:
var month = {
January : "01",
Febuary : "02",
March : "03",
April : "04",
May : "05",
June : "06",
July : "07",
August : "08",
September : "09",
October : "10",
November : "11",
December : "12"
}
function get_month(m){
return month[m];
}
How about: 怎么样:
function getMonth(mon){
var monthNum = new Date(mon +" 1, 2012").getMonth()+1;
return ("0" + monthNum).slice(-2);
}
Inspired from this SO post. 受此SO帖子启发。
Fiddle: http://jsfiddle.net/WFHWu/1/ 小提琴: http : //jsfiddle.net/WFHWu/1/
I would use a lookup here (or, an inverse-lookup as the case may be). 我将在此处使用查找(或视情况而定进行反向查找)。
The ternary ( ?:
) is not an appropriate direct replacement due the extreme amount of nesting it would require. 由于需要大量嵌套,因此三元( ?:
:) 不适合直接替换。
// The lookup, uses a dummy object (that will never match the indexOf)
// such that January is at index 1, etc.
var months = [{}, 'January', .., 'December'];
// This just returns a number, and expects the caller to format the number
// for display as appropriate. Returns 0 for an invalid month name.
function get_month(m) {
var monthNumber = months.indexOf(m); // -1 if not found
return monthNumber > 0 ? monthNumber : 0;
}
For your specific task, you could also use the in-build Date object : 对于您的特定任务,您还可以使用内置的Date对象 :
function get_month(m) {
return new Date(m + " 1").getMonth() + 1;
}
However, this will return "1" and not "01", so you would have to write a function that adds the first zero when needed. 但是,这将返回“ 1”而不是“ 01”,因此您必须编写一个在需要时添加第一个零的函数。
Use switch
: 使用switch
:
var ret;
switch (m) {
case 'January':
ret = '01';
break;
case 'Febuary':
ret = '02';
break;
...
}
Documentation . 文件资料 。
I'd recommend a switch statement instead. 我建议改用switch语句 。 Your other example is a ternary operator and would be even messier. 您的另一个示例是三元运算符,甚至会更加混乱。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.