简体   繁体   English

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

[英]Javascript get first value from array then skip it

Hey all this is the array I have so far: 嘿,这是我到目前为止拥有的所有数组:

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');
        }
    }
}

My issue is that I am only wanting to do this for the array: 我的问题是我只想对数组执行此操作:

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

In the above code I am wanting just to have a timeStart for the first day and an timeEnd for the second day etc etc. 在上面的代码,我想只是对第一天第二天等等等等,一个timeStarttimeEnd

The timeEnd for the 1st day should be static at 23:59:59 第一天timeEnd应该在23:59:59保持不变

The timeStart for the 2nd day should be static at 00:00:00 第二天timeStart应该在00:00:00保持静态

I am not sure how to go about doing that in my IF statement loop so help would be appreciated. 我不确定如何在IF语句循环中执行此操作,因此将不胜感激。

If I understand correctly ... You want to use the following array: 如果我正确理解...,您想使用以下数组:

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"}];

instead of the one you currently have. 而不是您当前拥有的那个。

In that case, you can increment the index i to get the next item in the array, for example: 在这种情况下,您可以增加索引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');
        }
    }
}

EDIT 编辑


I still don't fully understand what you're looking for but maybe look at this for a sec: 我仍然不太了解您要寻找的内容,但可能要花一秒钟时间:

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

The above will output to the console something like: 上面的内容将输出到控制台,例如:

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"

Each line consists of: 每行包括:

  1. The day , followed by; day ,接着是;
  2. The timeStart , followed by; timeStart ,后跟;
  3. The timeEnd 时间timeEnd

So if the day is 16, then the timeStart and timeEnd will be "23:00:00" and "11:00:00" , respectively. 因此,如果天是16,则timeStarttimeEnd将分别为"23:00:00""11:00:00"

But if the day is 17, then the timeStart and timeEnd will be "00:00:00" and "11:00:00" , respectively. 但是,如果天是17,则timeStarttimeEnd"00:00:00""11:00:00"

Etc, etc. 等等

The idea here is that I am checking whether timeEnd is defined during the loop. 这里的想法是我正在检查循环中是否definedtimeEnd

Maybe this will help you? 也许对您有帮助?


EDIT 编辑


If this is what the output should look like: 如果是这样,输出应如下所示:

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"

then you can go: 那么你可以去:

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

You can use map and then check the index to see it if's odd or even. 您可以使用map ,然后检查索引以查看它是奇数还是偶数。 Something like this: 像这样:

 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> 

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM