如何重新启动阵列



我有代码提醒当月和过去两个月:

 var cMonth = new Date().getMonth();
 var currentMonth, pastMonth2, pastMonth3 = 0
 var months = [
    "January", 
    "February", 
    "March", 
    "April", 
    "May", 
    "June",
    "July", 
    "August", 
    "September", 
    "October", 
    "November",
    "December"
  ]
    currentMonth = months[cMonth];
    pastMonth2 = months[cMonth - 1];
    pastMonth3 = months[cMonth - 2];
  alert(pastMonth3 + " " + pastMonth2 + " " + currentMonth);

但是,如果月份是 1 月或 2 月,我希望它"重新启动数组"并打印 12 月和 11 月,基本上打印数组的最后元素。我能想到的唯一解决方法是使用 if 语句添加验证,但它们无法正常工作,更不用说我觉得这不是一个合适的解决方案(如果而不是月是一个月中的几天,我想打印最后 12 天?

  if(cMonth == 1){ //prints Undefined Jan, Feb
    currentMonth = months[1]
    pastMonth2 = months[0]
    pastMonth3 = months[11]
  }
  if (cMonth == 0) { //prints Nov, Dec and Jan!
    currentMonth = months[0]
    pastMonth2 = months[11]
    pastMonth3 = months[10]
  } else {
    currentMonth = months[cMonth];
    pastMonth2 = months[cMonth - 1];
    pastMonth3 = months[cMonth - 2];
  }
  alert(pastMonth3 + " " + pastMonth2 + " " + currentMonth);

Date.toLocaleString 可用于获取月份名称:

const monthName = m => new Date(0, m).toLocaleString('en-US', { month: 'long' })
var month = new Date().getMonth();
console.log( monthName(month    ) ) // currentMonth 
console.log( monthName(month - 1) ) // pastMonth2
console.log( monthName(month - 2) ) // pastMonth3
console.log( monthName(-2) ) // November
console.log( monthName(13) ) // February

考虑一下模式:

cMonth      1  2  3  4  5  6  7  8  9  10 11 12
pastMonth3 11 12  1  2  3  4  5  6  7   8  9 10

则 cMonth(12( = 12 - 2 = 10。

和 cMonth(1( =

1 - (-10( = 11

所以一个函数可能是

  function pastMonth3(cMonth) {
    return cMonth - (9+cMonth); 
  }

这只会将索引返回到您的"月"数组中。 你可以像这样使用它:

 var month = months[pastMonth3(cMonth)];

取模运算符 (%( 为此完成
(==余数 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Remainder_(( (

const getMonthNumber = nMonth => (nMonth %12 +12) %12

有 12 个月,所以这是一个基于 == 12 (或 == 月.长度(
的系统 第一个+12是负数
第二个%12是正数

这个(箭头函数(就像:

function getMonthNumber(nMonth)
{
  let n = nMonth %12 
  n = n +12       // if n is negative
  return n %12    // if n is positive, so (n %12 +12) > 12 and we need to use a modulo again
}

示例代码 =>

var months = [ 'January',   'February', 'March',    'April'
             , 'May',       'June',     'July',     'August'
             , 'September', 'October',  'November', 'December'
             ]
const getMonthNumber = nMonth => (nMonth %12 +12) %12
var cMonth       = 0                 // January
  , currentMonth = months[ cMonth ]
  , pastMonth2   = months[ getMonthNumber( cMonth -1 )]
  , pastMonth3   = months[ getMonthNumber( cMonth -2 )] 
console.log(currentMonth , pastMonth2 , pastMonth3 )
console.log('month 17...', months[ getMonthNumber(   17 ) ])
console.log('month -6...', months[ getMonthNumber(   -6 ) ])
console.log('month 187..', months[ getMonthNumber(  187 ) ])
console.log('month -300.', months[ getMonthNumber( -300 ) ])

或者,将 12 添加到任何小于零的索引中,以将其旋转到正确的月份:

var currentMonth, pastMonth2, pastMonth3 = 0
    
     var months = [
        "January", 
        "February", 
        "March", 
        "April", 
        "May", 
        "June",
        "July", 
        "August", 
        "September", 
        "October", 
        "November",
        "December"
      ]
        i = months.indexOf("January")
        currentMonth = months[i]
        i = i-1;   
        pastMonth2 = months[i<0?i+12:i];
        i = i-1;
        pastMonth3 = months[i<0?i+12:i];
    
      console.log(pastMonth3, pastMonth2, currentMonth)

Array.slice支持这样的行为:

var months = [
    "January", 
    "February", 
    "March", 
    "April", 
    "May", 
    "June",
    "July", 
    "August", 
    "September", 
    "October", 
    "November",
    "December"
  ]
  
  var cMonth = 0; // january
  
  var currentMonth = months.slice(cMonth, cMonth + 1)[0];
  var pastMonth2 = months.slice(cMonth - 1)[0];
  var pastMonth3 = months.slice(cMonth - 2)[0];
  
  console.log(currentMonth, pastMonth2, pastMonth3)

这将记录January December November

最新更新