2015-05-12 40 views
0

我正在构建一个基于WordPress的应用程序,客户可以选择选择在过去一年中输入的数据。我很想让这个代码更聪明,但我不知道该怎么做。数据看起来JS是这样的:通过提供的日期如何比较干燥的月份?

idsh: Array[4] 
    0: "565" 
    1: "565" 
    2: "567" 
    3: "569" 
    length: 4 
datesh: Array[4] 
    0: "12/22/2014 - 00:00:00, 12:00 AM" 
    1: "01/21/2015 - 00:00:00, 12:00 AM" 
    2: "01/28/2015 - 00:00:00, 12:00 AM" 
    3: "02/04/2015 - 00:00:00, 12:00 AM" 
    length: 4 
ids: Array[5] 
    0: "1009" 
    1: "979" 
    2: "1009" 
    3: "1011" 
    4: "1013" 
    length: 5 
dates: Array[5] 
    0: "01/05/2015 - 00:00:00, 12:00 AM" 
    1: "12/22/2014 - 00:00:00, 12:00 AM" 
    2: "02/13/2015 - 00:00:00, 12:00 AM" 
    3: "02/20/2015 - 00:00:00, 12:00 AM" 
    4: "02/27/2015 - 00:00:00, 12:00 AM" 
    length: 5 

var getQuarters = (function() { 
 
     'use strict'; 
 
     var dates = [ 
 
     "01/05/2015 - 00:00:00, 12:00 AM", 
 
     "12/22/2014 - 00:00:00, 12:00 AM", 
 
     "02/13/2015 - 00:00:00, 12:00 AM", 
 
     "02/20/2015 - 00:00:00, 12:00 AM", 
 
     "02/27/2015 - 00:00:00, 12:00 AM" 
 
     ]; 
 

 
     function loops(items, fn, onLoopComplete) { 
 
     var i; 
 
     try { 
 
      if (items && items.length) { 
 
      i = items.length; 
 
      } else { 
 
      throw new Error(items + ' is required to have a length'); 
 
      } 
 

 
      if (i > -1) { 
 
      do { 
 
       if (items[i] !== undefined) { 
 
       fn(i); 
 
       /* console.log(i + ' is the current iteration'); */ 
 
       } 
 
      } 
 
      while (--i >= 0); 
 
      } 
 
      if (typeof onLoopComplete === 'function') { 
 
      onLoopComplete(items.length); 
 
      } 
 
     } catch (e) { 
 
      throw new Error(e); 
 
     } 
 
     } 
 

 

 
     function show3() { 
 
     loops(dates, function(i) { 
 
      var months = dates[i].slice(0, 2), 
 
      thisMonth = new Date().getMonth(); 
 

 
      if (months == thisMonth || 
 
      months == thisMonth - 1 || 
 
      months == thisMonth - 2) { 
 
      console.log(months); 
 
      } else { 
 
      console.log(dates[i] + ' do not meet criteria.'); 
 
      } 
 
     }); 
 
     } 
 

 
     function show6() { 
 
     loops(dates, function(i) { 
 
      var months = dates[i].slice(0, 2), 
 
      thisMonth = new Date().getMonth(); 
 

 
      if (months == thisMonth || 
 
      months == thisMonth - 1 || 
 
      months == thisMonth - 2 || 
 
      months == thisMonth - 3 || 
 
      months == thisMonth - 4 || 
 
      months == thisMonth - 5) { 
 
      console.log(months); 
 
      } else { 
 
      console.log(dates[i] + ' do not meet criteria.'); 
 
      } 
 
     }); 
 
     } 
 

 

 
     return function(quarters) { 
 
     switch (quarters) { 
 
      case 2: 
 
      show6(); 
 
      break; 
 
      case 3: 
 
      show9(); 
 
      break; 
 
      case 4: 
 
      show12(); 
 
      break; 
 
      default: 
 
      show3(); 
 
      break; 
 
     } 
 
     }; 
 
    }()); 
 

 
    getQuarters();

我循环,看看他们是否匹配的标准,(如最近3个月),但my code很现在重复。我如何让它变得更智能和干爽?

+0

1.您如何*显示*我们一些代码? 2.你想*干*代码?就像在“不做任何事情,只是打印你会做的事情”一样? – Siguza

+0

我建议你使用moment.js,它有助于减少代码。 – Sagar

+0

@Siguza请参阅jsbin的代码。 - 如果你错过了:http://jsbin.com/wuzegu/32/edit?js,console – colecmc

回答

1
function showLastMonths(numberOfMonths){ 
    loops(dates, function(i) { 
    var months = dates[i].slice(0, 2), 
    thisMonth = new Date().getMonth(); 

    monthDifferential = thisMonth - months 
    if (monthDifferential > 0 && monthDifferential <= numberOfMonths) { 
     console.log(months); 
     console.log(ids[i]); 
    } else { 
     console.log(dates[i] + ' do not meet criteria.'); 
    } 
    }); 
} 

这是我可以看到那里,只是提取参数,并删除show3 show6等功能,并调用,而不是最简单的重构。

+0

这是完美的@jfornoff!谢谢您的帮助。 – colecmc