简体   繁体   中英

How to shorten JavaScript if/else statements?

I have a function based on a lot of if/else statements. 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 .

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

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.

Fiddle: 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 :

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.

Use switch :

var ret;
switch (m) {
    case 'January':
        ret = '01';
        break;
    case 'Febuary':
        ret = '02';
        break;

    ...

}

Documentation .

I'd recommend a switch statement instead. Your other example is a ternary operator and would be even messier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM