1

I have an array arr = ['14:00', '15:00', '16:00', ...].

I want to rotate the array such that the time element which is closest to the actual time is first.

I have found a function to rotate an array here JavaScript Array rotate() which uses a function

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

But I don't know how to determine the count argument to ensure the first element is closest to the actual time.

Community
  • 1
  • 1
Jamgreen
  • 8,491
  • 24
  • 79
  • 186

3 Answers3

1

If you like to get the actual time to the top and the leave the rest the given order, you could use

  1. Array#splice for getting the wanted item and unshift it to the first place in the array.

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. Or sorting with moving the actual time to top with ordering the rest by string.

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);
Nina Scholz
  • 323,592
  • 20
  • 270
  • 324
0

To get the actual value you can use 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"]
rafaelcastrocouto
  • 10,518
  • 1
  • 34
  • 58
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));

Assuming that you are given the actual time in this format and the array with all timestamps from 00:00 to 23:00, you can do this. (I did not use the the function you referred to). I am not that comfortable with javascript yet, so if anyone can improve the code, let me know.

H. Low
  • 16
  • 3