gpt4 book ai didi

javascript - 分组时间间隔数组

转载 作者:行者123 更新时间:2023-12-02 14:25:45 25 4
gpt4 key购买 nike

我有一个生成时间间隔数组的代码。好吧,效果很好,得到了3分,开始时间,结束时间以及生成数组的时间(以分钟为单位)

  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", "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"]

我需要的是划分值数组,例如从 0900 到 1000。事实上这是 0900 09:00 10:00 但我删除了:。我需要的是对这些间隔时间表进行分组,输入 9 到 10、10 到 11 等等。有人可以帮忙吗?

["0900", "0911", "0922", "0933", "0944", "0955"]

["1000", "1006", "1017", "1028", "1039", "1050"]

最佳答案

您可以将原始数组缩减为映射或另一个数组。

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;

关于javascript - 分组时间间隔数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38267165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com