I have been stuck at this problem for a couple of days and couldn't figure it out.
Below is the data I have:
body = {
columns: {
links: [
"link1 ,\nlink2\n",
"link3 ,\nlink4\n"
],
timestamps: [
"[2:00-4:30, 5:53-7:23],\n[15:15-16:16, 45:43-47:32, 1:02:30-1:03:45]",
"[1:00-3:20, 8:50-9:20],\n[18:50-19:30, 42:00-46:28, 1:05:31-1:08:00]"
]
}
};
The timestamps for link1 would be 2:00-4:30 and 5:53-7:23, timestamps for link2 would be 15:15-16:16, 45:43-47:32, 1:02:30-1:03:45, however, they are in one string, same case with "link1, link2\n"
What I intended to do is as follow
For example:
body["columns"]["timestamps"][0] // "[2:00-4:30, 5:00-7:00],\n[15:15-16:16, 45:43-47:32, 1:02:30-1:03:45]"
timestamp_group = body["columns"][timestamps][0].match(/(?<=\[)(.*?)(?=\])/g)[0] // "[2:00-4:30, 5:00-7:00]"
//by using timestamp_group.split('-')
timestamp_group[] // ["2:00-4:30", "5:00-7:00"]
//further split into ["2:00", "4:30"]
//then using hmsToSecondsOnly() to make it into [120,270]
function hmsToSecondsOnly(str) {
var p = str.split(':'),
s = 0, m = 1;
while (p.length > 0) {
s += m * parseInt(p.pop(), 10);
m *= 60;
}
return s;
}
It would be something like this in the end:
[[120,270],[300, 420]],[[,],[,],[,]]
Then I can map links to time stamps.
body["columns"]["links"][0][0] to body["columns"]["timestamps"][0][0]
and calculate their durations.
I want to avoid using too much nested for loops, is there any better(elegant) way to approach this? Thanks.
I am not sure how to loop them properly. if using forEach, how should i start(nested or recursive?) Here are the attempts I tried in console.