Javascript从数组中获取第一个值然后跳过它

时间:2015-12-15 17:23:42

标签: javascript arrays

嘿,这就是我到目前为止所拥有的数组:

var thisDayDate = today.getDate();
var JANDays = [{day:16, timeStart:"23:00:00", timeEnd:"23:59:59"},  //It's Jan 16th Friday 11pm to
               {day:17, timeStart:"00:00:00", timeEnd:"11:00:00"},  //It's Jan 17th                Sat 11am
               {day:22, timeStart:"23:00:00", timeEnd:"23:59:59"},  //It's Jan 22nd Friday 11pm to  
               {day:23, timeStart:"00:00:00", timeEnd:"07:00:00"}]; //It's Jan 23ed                Sat  7am

for(var i = 0; i < JANDays.length; i++)
{
    if (thisDayDate == [JANDays[i].day]) {
        if (isSiteDown(JANDays[i].timeStart,JANDays[i].timeEnd)) { 
           showThePage(); 
        } else { 
           console.log('nope');
        }
    }
}

我的问题是我只想为数组做这个:

var JANDays = [{day:16, timeStart:"23:00:00"},  //It's Jan 16th Friday 11pm to
               {day:17, timeEnd:"11:00:00"},    //It's Jan 17th                Sat 11am
               {day:22, timeStart:"23:00:00"},  //It's Jan 22nd Friday 11pm to  
               {day:23, timeEnd:"07:00:00"}];   //It's Jan 23ed                Sat  7am

在上面的代码中,我只希望第一天有 timeStart ,第二天有 timeEnd 等等。

第一天 timeEnd 23:59:59

第二天 timeStart 00:00:00

时应为静态

我不知道如何在我的IF语句循环中这样做,所以请帮助。

3 个答案:

答案 0 :(得分:1)

如果我理解正确...你想使用以下数组:

var JANDays = [{day:16, timeStart:"23:00:00"},
               {day:17, timeEnd:"11:00:00"},
               {day:22, timeStart:"23:00:00"},
               {day:23, timeEnd:"07:00:00"}];

而不是你现在的那个。

在这种情况下,您可以递增索引i以获取数组中的下一个项目,例如:

var JANDays = [{day:16, timeStart:"23:00:00"},
               {day:17, timeEnd:"11:00:00"},
               {day:22, timeStart:"23:00:00"},
               {day:23, timeEnd:"07:00:00"}];

for (var i = 0; i < JANDays.length; i++)
{

    if (thisDayDate == JANDays[i].day) {

        // Notice "JANDays[i+1].timeEnd", this will get the timeEnd of the next item in the array
        // (Beware: i+1 could result in undefined)
        if (isSiteDown(JANDays[i].timeStart, JANDays[i+1].timeEnd)) { 
            showThePage(); 
        } else { 
            console.log('nope');
        }
    }
}

修改

我仍然不完全明白你在寻找什么,但也许可以看一下这个:

var JANDays = [{day:16, timeStart:"23:00:00"},
            {day:17, timeEnd:"11:00:00"},
            {day:22, timeStart:"23:00:00"},
            {day:23, timeEnd:"07:00:00"}];

for (var i = 0; i < JANDays.length; i++)
{
    var day = JANDays[i].day;
    var timeStart = JANDays[i] && JANDays[i].timeStart || "00:00:00";
    var timeEnd = JANDays[i] && JANDays[i].timeEnd;
    var nextTimeEnd = JANDays[i+1] && JANDays[i+1].timeEnd || "23:59:59";
    if (timeEnd) { // This indicates the second day
        console.log(day, timeStart, timeEnd);
    } else {
        console.log(day, timeStart, nextTimeEnd);
    }
}

以上内容将输出到控制台:

16 "23:00:00" "11:00:00"
17 "00:00:00" "11:00:00"
22 "23:00:00" "07:00:00"
23 "00:00:00" "07:00:00"

每一行包括:

  1. day,后跟;
  2. timeStart,后跟;
  3. timeEnd
  4. 因此,如果当天为16天,那么timeStarttimeEnd将分别为"23:00:00""11:00:00"

    但如果当天为17天,那么timeStarttimeEnd将分别为"00:00:00""11:00:00"

    Etc等。

    这里的想法是我在循环期间检查timeEnd是否为defined

    也许这会对你有帮助吗?

    修改

    如果这是输出的样子:

    16 "23:00:00" "23:59:59"
    17 "00:00:00" "11:00:00"
    22 "23:00:00" "23:59:59"
    23 "00:00:00" "07:00:00"
    

    然后你可以去:

    for (var i = 0; i < JANDays.length; i++)
    {
        var day = JANDays[i].day;
        // The code below will check to see if timeStart and timeEnd are defined
        // If they are defined then they will be used,
        // otherwise use the specified value after the pipes (||)
        var timeStart = (JANDays[i] && JANDays[i].timeStart) || "00:00:00";
        var timeEnd = (JANDays[i] && JANDays[i].timeEnd) || "23:59:59";
        if (thisDayDate == day) {
            console.log(day, timeStart, timeEnd);
        }
    }
    

答案 1 :(得分:0)

您可以使用map然后检查索引以查看它是奇数还是偶数。像这样:

var JANDays = [{
    day: 16,
    timeStart: "23:00:00",
    timeEnd: "24:59:59"
  }, //It's Jan 16th Friday 11pm to
  {
    day: 17,
    timeStart: "00:00:00",
    timeEnd: "11:00:00"
  }, //It's Jan 17th                Sat 11am
  {
    day: 22,
    timeStart: "23:00:00",
    timeEnd: "24:59:59"
  }, //It's Jan 22nd Friday 11pm to  
  {
    day: 23,
    timeStart: "00:00:00",
    timeEnd: "07:00:00"
  }
]; //It's Jan 23ed

var alt = JANDays.map(function(item,idx) {
    if (idx % 2) {
        return { day: item.day, timeStart: item.timeStart };
    }
    else {
        return { day: item.day, timeEnd: item.timeEnd };
    }
});

document.getElementById("output").innerHTML = JSON.stringify(alt);
<div id="output"></div>

答案 2 :(得分:0)

试试这个:

var JANDays = [{day:16, timeStart:"23:00:00", timeEnd:"24:59:59"},  //It's Jan 16th Friday 11pm to
               {day:17, timeStart:"00:00:00", timeEnd:"11:00:00"},  //It's Jan 17th                Sat 11am
               {day:22, timeStart:"23:00:00", timeEnd:"24:59:59"},  //It's Jan 22nd Friday 11pm to  
               {day:23, timeStart:"00:00:00", timeEnd:"07:00:00"}]; //It's Jan 23ed                Sat  7am

var output = JANDays.map(function(item, index){
  var temp = {};
  temp.day = item.day;
  
  if(index%2 == 0){
    temp.timeStart = item.timeStart;
  }
  else{
    temp.timeEnd = item.timeEnd;
  }
  return temp;
})

console.log(output);