Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何重新启动阵列?_Javascript_Arrays - Fatal编程技术网

Javascript 如何重新启动阵列?

Javascript 如何重新启动阵列?,javascript,arrays,Javascript,Arrays,我有一段代码,可在当月加上最近两个月发出警报: var cMonth = new Date().getMonth(); var currentMonth, pastMonth2, pastMonth3 = 0 var months = [ "January", "February", "March", "April", "May", "June", "July", "August", "Septembe

我有一段代码,可在当月加上最近两个月发出警报:

 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);
但是,如果月份是一月或二月,我希望它“重新启动阵列”并打印十二月和十一月,基本上打印阵列的最后元素。我能想到的唯一修复方法是使用if语句添加验证,但它们不能正常工作,更不用说我觉得这不是一个适当的解决方案(如果不是月份而是月份中的几天,我想打印最后12天怎么办?)


Array.slice
支持这种行为:

var月=[
“一月”,
“二月”,
“三月”,
“四月”,
“五月”,
“六月”,
“七月”,
“八月”,
“9月”,
“十月”,
“11月”,
“12月”
]
变量cMonth=0;//一月
var currentMonth=months.slice(cMonth,cMonth+1)[0];
var pastMonth2=月数。切片(厘米月-1)[0];
var pastMonth3=months.slice(cMonth-2)[0];

console.log(currentMonth、pastMonth2、pastMonth3)
考虑一下模式:

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)];

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

var currentmount,passmonth2,passmonth3=0
风险值月份=[
“一月”,
“二月”,
“三月”,
“四月”,
“五月”,
“六月”,
“七月”,
“八月”,
“9月”,
“十月”,
“11月”,
“12月”
]
i=月份指数(“一月”)
currentMonth=月份[i]
i=i-1;
pastMonth2=月[i为此执行模运算(%)
(==余数)

有12个月,因此这是一个基于==12(或==months.length)的系统
第一个
+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=[“一月”、“二月”、“三月”、“四月”
“五月”、“六月”、“七月”、“八月”
“九月”、“十月”、“十一月”、“十二月”
]
常量getMonthNumber=nMonth=>(nMonth%12+12)%12
var cMonth=0//1月
,currentMonth=月[cmmonth]
,pastMonth2=月[getMonthNumber(cMonth-1)]
,pastMonth3=月[getMonthNumber(cMonth-2)]
console.log(当前月、过去月2、过去月3)
console.log('month 17…',month[getMonthNumber(17)])
log('month-6…',month[getMonthNumber(-6)])
console.log('187个月..',个月[getMonthNumber(187)])
console.log('month-300',month[getMonthNumber(-300)])
可用于获取月份名称:

const monthName=m=>新日期(0,m).toLocaleString('en-US',{month:'long'})
var month=新日期().getMonth();
console.log(monthName(month))//currentMonth
log(monthName(month-1))//passmonth2
log(monthName(第2个月))//passmonth3
console.log(monthName(-2))//11月

console.log(monthName(13))//二月
lol,三元索引:p(顺便说一句,它本身没有错,我只需要一秒钟的时间来解析代码)既然函数是-9代表1,那么负数在数组中是如何工作的?
1-(9+1)
还是我没有得到这个?看起来你只是在寻找
(cMonth+0)%12
(cMonth+11)%12
,和
(cMonth+10)%12
,用于索引?对于那些像我一样疑惑的人,当你给slice提供负数时,它会从字符串长度中减去数字。
const getMonthNumber = nMonth => (nMonth %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
}