Javascript(Typescript):for循环无法正常工作

Javascript(Typescript):for循环无法正常工作,javascript,arrays,typescript,Javascript,Arrays,Typescript,我在爱奥尼亚2号上工作,我试图做一个for循环来显示某些数据。但它的行为非常奇怪 我目前使用moment.js还有一系列剩余时间 var dur = moment.duration( moment(date).diff(moment()) ); let yearsRemain = dur.years(); let monthsRemain = dur.months(); let daysRemain = dur.days(); let hoursRemain = dur.hours(); let

我在爱奥尼亚2号上工作,我试图做一个for循环来显示某些数据。但它的行为非常奇怪

我目前使用moment.js还有一系列剩余时间

var dur = moment.duration( moment(date).diff(moment()) );
let yearsRemain = dur.years();
let monthsRemain = dur.months();
let daysRemain = dur.days();
let hoursRemain = dur.hours();
let minutesRemain = dur.minutes();
let secondsRemain = dur.seconds();

var dateArray = [
  yearsRemain,
  monthsRemain,
  daysRemain,
  hoursRemain,
  minutesRemain,
  secondsRemain
]
如果我像这样输出dateArray.timeString=dateArray.join,;并在我的html中显示时间字符串,我可以看到以下值:

0,  0,  0,-17,-46, -3  //17 hours ago
0,  0,  0, 10,  7, 47  //in 10 hours 7 minutes
0,  0,  2,  1,  9, 35  //in 2 days and 1 hour
现在,我遍历数组并尝试获得两个最大的值。我试图显示一个字符串,就像我在上面的评论中所做的那样。如果是在过去,我只想显示最大值

for(var i = 0; i < dateArray.length; i++) {
  if(dateArray[i] > 0){
    //If the event is in the future
    this.state = "future";
    this.timePrimary = dateArray[i];
    this.timePrimaryType = this.typeOfTime(i, dateArray[i]);
    this.timeSecondary = dateArray[i+1] !== 0 ? dateArray[i+1] : dateArray[i+2];
    this.timeSecondaryType = dateArray[i+1] !== 0 ? this.typeOfTime(i+1, dateArray[i+1]) : this.typeOfTime(i+2, dateArray[i+2]);
    break;
  } else if(dateArray[i] < 0) {
    //If the event is in the past
    this.state = "past";
    this.timePrimary = dateArray[i] * (-1);
    this.timePrimaryType = this.typeOfTime(i, (dateArray[i] * (-1))) + " ago";
    break;
  } else {
    i++
  }
}
}这是因为代码中的else语句使计数器i递增,这导致循环在遇到0时跳过数组中的下一项。 这就是为什么在0之后跳过10。 拆下那个零件,它就会工作

for(var i = 0; i < dateArray.length; i++) {
  if(dateArray[i] > 0){
    //If the event is in the future
    this.state = "future";
    this.timePrimary = dateArray[i];
    this.timePrimaryType = this.typeOfTime(i, dateArray[i]);
    this.timeSecondary = dateArray[i+1] !== 0 ? dateArray[i+1] : dateArray[i+2];
    this.timeSecondaryType = dateArray[i+1] !== 0 ? this.typeOfTime(i+1, dateArray[i+1]) : this.typeOfTime(i+2, dateArray[i+2]);
    break;
  } else if(dateArray[i] < 0) {
    //If the event is in the past
    this.state = "past";
    this.timePrimary = dateArray[i] * (-1);
    this.timePrimaryType = this.typeOfTime(i, (dateArray[i] * (-1))) + " ago";
    break;
  } else {
    //do nothing... let the loop to go on.
  }
}

为什么要在循环的底部递增i?@torazaburo else语句基本上是说,如果这个数组项的值等于0,请查看下一个数组item@torazaburo,你的评论完全正确。我有史以来最大的大脑放屁。没有理由在for循环中增加循环。。。非常感谢。也许是或者类似的?@Colosos博士,我试过用它,效果很好,但我想看两次最大的。从现在起,只显示单个最大时间。哇。那是一个脸掌在那里。。。感谢您再次帮助我理解基本循环!答案将在7分钟内被接受
for(var i = 0; i < dateArray.length; i++) {
  if(dateArray[i] > 0){
    //If the event is in the future
    this.state = "future";
    this.timePrimary = dateArray[i];
    this.timePrimaryType = this.typeOfTime(i, dateArray[i]);
    this.timeSecondary = dateArray[i+1] !== 0 ? dateArray[i+1] : dateArray[i+2];
    this.timeSecondaryType = dateArray[i+1] !== 0 ? this.typeOfTime(i+1, dateArray[i+1]) : this.typeOfTime(i+2, dateArray[i+2]);
    break;
  } else if(dateArray[i] < 0) {
    //If the event is in the past
    this.state = "past";
    this.timePrimary = dateArray[i] * (-1);
    this.timePrimaryType = this.typeOfTime(i, (dateArray[i] * (-1))) + " ago";
    break;
  } else {
    //do nothing... let the loop to go on.
  }
}