2013-05-22 23 views
14

我有以下值(例如)陣列:的Javascript獲取日期順序在陣列

[ 
     1367848800000: true, 
     1367935200000: true, 
     1368021600000: true, 
     1368108000000: true, 
     1368194400000: true, 
     1368367200000: true, 
     1368540000000: true, 
     1368626400000: true, 
     1368712800000: true 
    ] 

其中指數是一個日期時間。日期時間將始終在日期的12:00:00。

在這個例子中,前五個日期是連續的,然後是一天本身,然後是另一組3個日期。下面是我的意思的一個例子。現在

Dates on Calendar

,我所要做的是找到連續的日期,並把它們放到一個數組如下:

[ 
     1367848800000, 
     1367935200000, 
     1368021600000, 
     1368108000000, 
     1368194400000 
    ], 
    [ 
     1368367200000, 
     1368540000000, 
     1368626400000, 
    ], 
    [ 
     1368712800000Ω 
    ] 

所以最後,我有一個數組,用3個陣列在所有的時代。 我已經嘗試過無數代碼,但是一切都出來了,沒有什麼值得在這裏發佈。任何幫助將非常感激!

+2

您應該發佈這些__衆多的code_之一。 –

+7

我們應該多投這種格式良好,解釋得很好的問題! +1 –

+0

你的榜樣會產生你四個陣列作爲第二個數組中的前兩個日期之間的差值是兩天前 – Andreas

回答

2

以下方法使用陣列.reduce()方法:

var arr = [1367848800000, 1367935200000, 1368021600000, 
      1368108000000, 1368194400000, 1368367200000, 
      1368540000000, 1368626400000, 1368712800000], 
    i = 0, 
    result = arr.reduce(function(stack, b) { 
     var cur = stack[i], 
      a = cur ? cur[cur.length-1] : 0; 

     if (b - a > 86400000) { 
      i++; 
     } 

     if (!stack[i]) 
      stack[i] = []; 

     stack[i].push(b); 

     return stack; 
    }, []); 

console.log(result); 

DEMO:http://jsfiddle.net/gbC8B/1/

+0

完美地工作!我選擇你的,因爲它看起來很乾淨簡單。非常感謝你。 – MichaelH

+0

@MichaelH不客氣!我忘記提及*古代*瀏覽器可能不支持'.reduce()'數組,* MDN *建議使用[shim兼容性](https://developer.mozilla.org/en-US/) docs/JavaScript/Reference/Global_Objects/Array/Reduce#Compatibility)(如果你確實需要的話)。 – VisioN

+0

幸運的是,我們只支持現代瀏覽器。乾杯。 – MichaelH

1

某事像這樣可以這樣做:

function sequentialize(dArr) { 
     dArr = Object.keys(dArr).slice().sort(); 
     var last; 
     var arrs = [[]]; 

     for (var i = 0, l = dArr.length; i < l; i++) { 
      var cur = new Date(); 
      cur.setTime(dArr[i]); 
      last = last || cur; 

      if (isNewSequence(cur, last)) { 
       arrs.push([]); 
      } 

      arrs[arrs.length - 1].push(cur.getTime()); //always push to the last index 
      last = cur; 
     } 


     return arrs; 


     function isNewSequence(a, b) { 
      if (a.getTime() - b.getTime() > (24 * 60 * 60 * 1000)) 
       return true; 
      return false; 
     } 
    } 

現在,如果你通過例如Array/Object到sequentialize功能

var dates = { 
     1367848800000: true, 
     1367935200000: true, 
     1368021600000: true, 
     1368108000000: true, 
     1368194400000: true, 
     1368367200000: true, 
     1368540000000: true, 
     1368626400000: true, 
     1368712800000: true 
    }; 

    console.log(sequentialize(dates)); 

這讓下面的輸出

[ 
     [ 
      1367848800000, 
      1367935200000, 
      1368021600000, 
      1368108000000, 
      1368194400000 
     ], 
     [ 
      1368367200000 
     ], 
     [ 
      1368540000000, 
      1368626400000, 
      1368712800000 
     ] 
    ] 

這只是

  1. 創建一個數組了日期鍵,

  2. 排序他們

  3. 迭代在他們

  4. 如果當前和最後的差日期大於一天

  5. 的序列陣推新陣的次序陣列

  6. 按當前日期到最後陣列

    演示上JSBin

注:您可能需要改變isNewSequence功能實際符合您的需求

1
// Preconditions: singleArray contains the input array with each element corresponding to a time index. singleArray is sorted. 

var outputArray = new Array(); 
var stack = new Array(); 
var stackSize = 0; 

var i; 
for(i = 0; i < singleArray.length; i++) 
{ 
    // Get the last element on the stack 
    var lastElement = (stackSize == 0) ? 0 : stack.pop(); 

    // Compare to see if difference is one day 
    if(singleArray[i] - lastElement == 86400000) // 24 * 60 * 60 * 1000 
    { 
     // Dates are 1 day apart 
     if(lastElement != 0) stack.push(lastElement); 
     stack.push(singleArray[i]); 
     stackSize++; 
    } 
    else 
    { 
     if(lastElement != 0) stack.push(lastElement); 

     var tempQueue = new Array(); 
     while(stackSize > 0) 
     { 
      // Build up a new array containing consecutive days 
      // using a queue 
      tempQueue.push(stack.pop()); 
      stackSize--; 
     } 

     // Push the consecutive days onto the next place in the output array. 
     outputArray.push(tempQueue); 

     // Start a new group of consecutive dates 
     stack.push(singleArray[i]); 
     stackSize++; 
    } 

} 
+0

不幸的是JavaScript沒有偷看方法,所以我們要推的最後一個元素重新 – ose

1

愛是愛這些難題。尼斯回答每個人,這是我的更多jQuery的方法。

var datearray = { 
    1367848800000: true, 
    1367935200000: true, 
    1368021600000: true, 
    1368108000000: true, 
    1368194400000: true, 
    1368367200000: true, 
    1368540000000: true, 
    1368626400000: true, 
    1368712800000: true 
}; 

$(function() { 

    var result = dateSequences(datearray); 
} 

function dateSequences(array) { 
    // parse json object to array of keys 
    var keys = Object.keys(array); 
    // sort it up 
    keys = keys.sort(); 
    // convert them to dates 
    var dates = new Array(); 
    $.each(keys, function(i) { 
     dates.push(new Date(parseInt(keys[i]))); 
    }); 

    // now we have array of dates, search for sequential dates 
    var final = new Array(); 
    var prevdate = undefined; 
    var currentseq = 0;  
    $.each(dates, function(i, d) { 
     // undefined? 
     // first sequence 
     if (prevdate == undefined) { 
      final.push(new Array()); 
      final[currentseq].push(d); 
     } 
     else { 
      // compare if difference to current date in loop is greater than a day 
      var comp=new Date(); 
      comp.setDate(prevdate.getDate()+2); 
      // Advance sequence if it is 
      if (comp < d) { 
       currentseq++; 
       final[currentseq] = new Array(); 
      } 
      // Push the date to current sequence 
      final[currentseq].push(d);    
     } 
     // store previous 
     prevdate = d; 
    }); 

    return final; 
} 

小提琴:

http://jsfiddle.net/f57Ah/1/

1

試圖陣列sortforEach

var dates = [1367848800000, 1367935200000, 1368021600000, 
      1368108000000, 1368194400000, 1368367200000, 
      1368540000000, 1368626400000, 1368712800000]; 

var k = 0 , sorted = [[]]; 

dates.sort(function (a, b){ 

    return +a > +b ? 1 : +a == +b ? 0: -1; 
}) 
.forEach(function(v , i){ 

    var a = v,b = dates[i+1]||0; 

    sorted[k].push(+a); 

    if ((+b - +a) > 86400000) { 
      sorted[++k] = [] 
    } 
}); 

以後可以每數進行排序

sorted.sort(function (a,b){ 
    return a.length > b.length ? -1: 1; 
}); 

sorted數組包含所需的結果jsfiddle