2017-05-20 7 views
1

Ich habe ein Array arr = ['14:00', '15:00', '16:00', ...].Drehen Sie ein Array durch übereinstimmende Elementwerte

Ich möchte das Array so drehen, dass das Zeitelement, das der tatsächlichen Zeit am nächsten ist, zuerst ist.

Ich habe eine Funktion gefunden ein Array hier JavaScript Array rotate() zu drehen, die eine Funktion

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

verwendet Aber ich weiß nicht, wie die Zählung Argument, um zu bestimmen das erste Element, um sicherzustellen, zu der aktuellen Zeit am nächsten ist.

+0

was meinst du mit * "rotieren" *? nur um die tatsächliche Zeit nach vorne zu stellen? –

Antwort

1

Wenn Sie die aktuelle Zeit an die Spitze bekommen und die den Rest der angegebenen Reihenfolge verlassen, könnten Sie

  1. Array#splice für das Erhalten der gewünschten Artikel verwenden und an erster Stelle in der Anordnung unshift.

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

  1. oder Sortieren mit der aktuellen Zeit von String mit der Bestellung des Restes nach oben zu bewegen.

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

Um den aktuellen Wert erhalten Sie 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));
verwenden können 10

Angenommen, Sie erhalten die tatsächliche Uhrzeit in diesem Format und das Array mit allen Zeitstempeln von 00:00 bis 23:00 Uhr, können Sie dies tun. (Ich habe die Funktion, auf die du dich bezogen hast, nicht benutzt). Ich bin noch nicht so vertraut mit Javascript, also wenn jemand den Code verbessern kann, lass es mich wissen.