我有一个生成时间间隔数组的代码。好吧,它运行良好,它得到3分,开始时间,结束和生成数组的时间(分钟)
known_hosts
它会生成这个数组:
function GeraHorario(inicio,fim,tempoConsulta)
{
var consultas = Math.floor(60/tempoConsulta);
for (var i = 0; i <= consultas; i++) {
$scope.consultasHora.push(i)
}
var agenda = []
var data = new Date(2016, 0, 1, inicio, 0, 0, 0)
var horainicial = inicio
while (data.getDate() == 1) {
if(data.getHours() < fim)
{
var horario = data.toLocaleTimeString('pt-BR');
data.setMinutes(data.getMinutes()+tempoConsulta);
//agenda.push(horario.replace(/(.*)\D\d+/, '$1'));
var d1 = horario.replace(/(.*)\D\d+/, '$1');
agenda.push(d1.replace(/:/g,''));
}else{break}
}
console.log(agenda)
return agenda
}
我需要的是划分值数组,例如从0900到1000。 那实际上这是0900 09:00 10:00然而我删除了:我需要的是对这些间隔时间表进行分组,输入9到10,10到11等等。有人可以帮忙吗?
["0900", "0911", "0922", "0933", "0944", "0955", "1000", "1006", "1017", "1028", "1039", "1050", "1100", "1101", "1112", "1123", "1134", "1145", "1156", "1200", "1207", "1218", "1229", "1240", "1251", "1300", "1302", "1313", "1324", "1335", "1346", "1357", "1400", "1408", "1419", "1430", "1441", "1452", "1500", "1503", "1514", "1525", "1536", "1547", "1558", "1600", "1609", "1620", "1631", "1642", "1653", "1700", "1704", "1715", "1726", "1737", "1748", "1759", "1800", "1810", "1821", "1832", "1843", "1854", "1900"]
答案 0 :(得分:2)
您可以将原始数组缩减为地图或其他数组。
var result = agenda.reduce(function( map, hourStr ) {
var hour = hourStr.charAt(0) + hourStr.charAt(1);
if (!map.hasOwnProperty(hour)) map[hour] = [];
map[hour].push(hourStr);
return map;
}, {});
二维数组版本:
var result = agenda.sort().reduce(function( map, hourStr ) {
var hour = hourStr.charAt(0) + hourStr.charAt(1);
if (!map.indices.hasOwnProperty(hour)) {
map.indices[hour] = map.ary.length;
map.ary.push([]);
}
map.ary[map.indices[hour]].push(hourStr);
return map;
}, {
'indices' : {},
'ary' : []
}).ary;
答案 1 :(得分:0)
这是一个非字符串方法:
var groups = {};
for(var index in agenda){
var time = times[index];
var groupId = Math.floor(parseInt(time) / 100);
if(!groups[groupId]) groups[groupId] = [];
groups[groupId].push(time);
}