2017-05-20 47 views
1

我有一個數組arr = ['14:00', '15:00', '16:00', ...]通過匹配元素值來旋轉數組

我想旋轉數組,使得最接近實際時間的時間元素是第一個。

我發現一個函數來這裏旋轉陣列JavaScript Array rotate()它採用了功能

function arrayRotate(arr, count) { 
    count -= arr.length * Math.floor(count/arr.length) 
    arr.push.apply(arr, arr.splice(0, count)) 
    return arr 
} 

但我不知道如何確定count參數,以保證第一個元素是最接近實際時間。

+0

你是什麼意思*「旋轉」*?只是把實際的時間放在前面? –

回答

1

如果你想了解的實際時間頂部和離開,其餘的給定的順序,你可以使用

  1. Array#splice爲獲得想要的項目,它不印字到陣列擺在首位。

var array = ['14:00', '15:00', '16:00', '17:00'], 
 
    actual = '16:00'; 
 

 
array.unshift(array.splice(array.indexOf(actual), 1)[0]); 
 
console.log(array);

  • 或用移動的實際時間與由字符串排序的其餘部分到頂部排序。
  • var array = ['14:00', '15:00', '16:00', '17:00'], 
     
        actual = '16:00'; 
     
    
     
    array.sort(function (a, b) { 
     
        return (b === actual) - (a === actual) || a.localeCompare(b); 
     
    }); 
     
    
     
    console.log(array);

    0

    獲得實際的價值,你可以使用new Date().getHours()

    let arr = []; 
     
         
     
    for (var i=0; i<24;i++) { 
     
        arr[i] = i+':00'; 
     
    } 
     
    
     
    // console.log(arr) ["0:00", "1:00", "2:00", "3:00", "4:00", "5:00", "6:00", "7:00", "8:00", "9:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00"] 
     
    
     
    function arrayRotate(arr, count) { 
     
        count -= arr.length * Math.floor(count/arr.length) 
     
        arr.push.apply(arr, arr.splice(0, count)) 
     
        return arr 
     
    } 
     
    
     
    let hour = new Date().getHours(); 
     
    
     
    /// console.log(hour); 12 
     
    
     
    arrayRotate(arr, hour); 
     
    
     
    // console.log(arr); ["12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00", "0:00", "1:00", "2:00", "3:00", "4:00", "5:00", "6:00", "7:00", "8:00", "9:00", "10:00", "11:00"]

    0

    var array = ["0:00", "1:00", "2:00", "3:00", "4:00", "5:00", "6:00", "7:00", "8:00", "9:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00"]; 
     
        var actual = '16:21'; 
     
    
     
        // calculates the difference between 2 timestamps in hh:mm format 
     
        function timeDifference(time1, time2){ 
     
        var t1 = time1.split(':'), t2 = time2.split(':'); 
     
        return Math.abs((t1[0]*60 + t1[1]) - (t2[0]*60 + t2[1])); 
     
        } 
     
    
     
        // loops over the array and compares each time the first element with the actual time 
     
        // until a timestamp is found where the difference between the actual time and that one 
     
        // is less or equal to 30 minutes 
     
        function closestTime(array, actual){ 
     
        var diff = timeDifference(actual, array[0]); 
     
        var i = 1; 
     
        while (i < array.length && diff > 30) { 
     
         // add the first element to the end of the array 
     
         var b = array.shift(); 
     
         array.push(b); 
     
         
     
         diff = timeDifference(actual, array[0]); 
     
         i += 1; 
     
        } 
     
        return array; 
     
        } 
     
    
     
        console.log(closestTime(array, actual));

    假設您給出了這種格式的實際時間和所有時間戳從00:00到23:00的數組,您可以這樣做。 (我沒有使用你提到的功能)。我對JavaScript並不是很滿意,所以如果任何人都可以改進代碼,請告訴我。