Help with a function - time format "hh:mm" at the start of every array

Hey there,

Hope someone can give me a hand.

That function I have:

let formattedOutput = [];
let tempArray = [];
let chunkSize = 4; // Each game has 4 elements (time, hometeam, awayteam, channel)

for (let i = 0; i < msg.payload.length; i++) {
    tempArray.push(msg.payload[i]);
    if ((i + 1) % chunkSize === 0) {
        formattedOutput.push(tempArray);
        tempArray = [];
    }
}

let finalOutput = formattedOutput.map(game => {
    return game[0] + " " + game[1] + " VS " + game[2] + " " + game[3];
});

msg.payload = finalOutput.join('\n');
return msg;

Outputs a string:

msg.payload : string[6619]
"  18:45 Portimonense  FC Porto  SPORT TV1    19:00 Go Ahead Eagles  PSV SPORT TV2    19:30 Estugarda  Union Berlin ELEVEN SPORTS 2    19:30 SD Huesca  FC Andorra ELEVEN SPORTS 5    19:45 Napoli  Torino SPORT TV4    20:00 Barcelona  Mallorca ELEVEN SPORTS 1    20:00 Nice  Montpellier ELEVEN SPORTS 4    20:45 CF Estrela Amadora  Casa Pia SPORT TV2    20:45 U. Leiria SAD  Oliveirense SPORT TV3    22:00 Caxias  SĂŁo JosĂ©-RS PFC      11:00 FC Porto S19  Benfica S19 PORTO CANAL    11:00 Mafra  Penafiel SPORT TV1    11:00 Sp. Braga FM  Lank Vilaverdense FM CANAL 11    11:00 Sporting S17  V. GuimarĂŁes S17 SPORTING TV    12:00 Holstein Kiel  Karlsruher ELEVEN SPORTS 4    12:00 Schalke 04  SC Paderborn 07 ELEVEN SPORTS 3    12:30 Man Utd  Everton ELEVEN SPORTS 1    13:00 ValĂȘncia  Getafe ELEVEN SPORTS 2    14:00 Nacional  Ac. Viseu SPORT TV+    14:30 Bayern MĂŒnchen  Mainz ELEVEN SPORTS 3    14:30 RB Leipzig  SV Darmstadt 98 ELEVEN SPORTS 4    15:00 Torreense FM  Sporting FM SPORT TV6    15:00 V. ..."

What I want it to do, is to output as arrays; every time there's a format "hh:mm", it creates a new array.

Something like:
array[1]
18:45 Portimonense Vs FC Porto SPORT TV1
array[2]
19:00 Go Ahead Eagles Vs PSV SPORT TV2
array[3]
19:30 Estugarda Vs Union Berlin ELEVEN SPORTS 2
array[4]
19:30 SD Huesca Vs FC Andorra ELEVEN SPORTS 5
array[5]
19:45 Napoli Vs Torino SPORT TV4
...

Any ideas?

Regex match() String.prototype.match() - JavaScript | MDN
e.g.

[{"id":"9ae215f3a12ef293","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"18:45 Portimonense  FC Porto  SPORT TV1    19:00 Go Ahead Eagles  PSV SPORT TV2    19:30 Estugarda  Union Berlin ELEVEN SPORTS 2    19:30 SD Huesca  FC Andorra ELEVEN SPORTS 5    19:45 Napoli  Torino SPORT TV4    20:00 Barcelona  Mallorca ELEVEN SPORTS 1    20:00 Nice  Montpellier ELEVEN SPORTS 4    20:45 CF Estrela Amadora  Casa Pia SPORT TV2    20:45 U. Leiria SAD  Oliveirense SPORT TV3    22:00 Caxias  SĂŁo JosĂ©-RS PFC      11:00 FC Porto S19  Benfica S19 PORTO CANAL    11:00 Mafra  Penafiel SPORT TV1    11:00 Sp. Braga FM  Lank Vilaverdense FM CANAL 11    11:00 Sporting S17  V. GuimarĂŁes S17 SPORTING TV    12:00 Holstein Kiel  Karlsruher ELEVEN SPORTS 4    12:00 Schalke 04  SC Paderborn 07 ELEVEN SPORTS 3    12:30 Man Utd  Everton ELEVEN SPORTS 1    13:00 ValĂȘncia  Getafe ELEVEN SPORTS 2    14:00 Nacional  Ac. Viseu SPORT TV+    14:30 Bayern MĂŒnchen  Mainz ELEVEN SPORTS 3    14:30 RB Leipzig  SV Darmstadt 98 ELEVEN SPORTS 4    15:00 Torreense FM  Sporting FM SPORT TV6","payloadType":"str","x":510,"y":7600,"wires":[["cfa95e7a06cbd6ae"]]},{"id":"cfa95e7a06cbd6ae","type":"function","z":"d1395164b4eec73e","name":"function 156","func":"msg.payload = msg.payload.match(/\\d\\d:\\d\\d[^\\d\\d:\\d\\d]*/g)\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":7600,"wires":[["902a652132b37f08"]]},{"id":"902a652132b37f08","type":"debug","z":"d1395164b4eec73e","name":"debug 2484","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":750,"y":7600,"wires":[]}]
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.