从多维数组中获取下一个最小值
我有一个多维数组(json 编码,那么这仍然是正确的术语吗?)
{"tasks":[{"task":"first parent task", "sub-tasks":[{"sub-task":"first sub task", "startTime":0, "endTime":30}, {"sub-task":"second sub task", "startTime":65, "endTime":90}]}, {"task":"second parent task", "sub-tasks":[{"sub-task":"task 2 sub task 1", "startTime":35, "endTime":45},{"sub-task":"task 2 sub task 2", "startTime":95, "endTime":100}]}]}
我有一些 javascript 来获取当前的子任务,并且我试图在开始时间之前获取下一个子任务,但是对于出于某种原因,我只能在同一父任务中获得下一个子任务。 因此,我正在寻找的输出是
first parent task - first sub task -> nextTask = second parent task - task 2 sub task 1 -> nextTask = first parent task -> second sub task -> nextTask = second parent task - task 2 sub task 2
我当前拥有的代码,但它只为我提供同一数组中的子任务。
for(var r=0;r<tasks.length;r++){ for (var i = 0; i<tasks[r].sub-tasks.length){ if(tasks[r].sub-tasks[i].startTime==currenttimer){ showTask(tasks[r].sub-tasks[i], tasks); } } } function showTask(current, tasks){ jQuery('div#currentTask').text(current.sub-task); var nextStep = current+200; // just to set the next beyond the length of all tasks for (var nt=0; nt<tasks.length; nt++){ for (var nst=0; nst<tasks[nt].sub-tasks.length; nt++){ if(tasks[nt].sub-tasks[nst].startTime<nextStep && tasks[nt].sub-tasks[nst].startTime>current.startTime){ jQuery('div#nextTask').text(tasks[nt].sub-tasks[nst].sub-task); } } } }
该代码仅返回第一个父任务子任务,但我不确定为什么。有更好的方法吗?
最初的 for(var r... 语句在画布上绘制的另一个循环中运行,因此我以这种方式运行循环,以避免每秒或更长时间更新 DOM。
我不想将 startTime 放入单独的数组或任何内容中就像这样,因为我需要获取该数组并引用原始 json ,这看起来很浪费。
I've got a multi-dimensional array (json encoded, so is that still the right terminology?)
{"tasks":[{"task":"first parent task", "sub-tasks":[{"sub-task":"first sub task", "startTime":0, "endTime":30}, {"sub-task":"second sub task", "startTime":65, "endTime":90}]}, {"task":"second parent task", "sub-tasks":[{"sub-task":"task 2 sub task 1", "startTime":35, "endTime":45},{"sub-task":"task 2 sub task 2", "startTime":95, "endTime":100}]}]}
I have some javascript to get the current sub-task and I'm trying to get the next sub-task by starttime, but for some reason, i only get the next sub-task in the same parent task.
So what I'm looking for the output to be is
first parent task - first sub task -> nextTask = second parent task - task 2 sub task 1 -> nextTask = first parent task -> second sub task -> nextTask = second parent task - task 2 sub task 2
here is the code I currently have, but it only gives me sub tasks from within the same array.
for(var r=0;r<tasks.length;r++){ for (var i = 0; i<tasks[r].sub-tasks.length){ if(tasks[r].sub-tasks[i].startTime==currenttimer){ showTask(tasks[r].sub-tasks[i], tasks); } } } function showTask(current, tasks){ jQuery('div#currentTask').text(current.sub-task); var nextStep = current+200; // just to set the next beyond the length of all tasks for (var nt=0; nt<tasks.length; nt++){ for (var nst=0; nst<tasks[nt].sub-tasks.length; nt++){ if(tasks[nt].sub-tasks[nst].startTime<nextStep && tasks[nt].sub-tasks[nst].startTime>current.startTime){ jQuery('div#nextTask').text(tasks[nt].sub-tasks[nst].sub-task); } } } }
That code returns only first parent task sub-tasks, but I'm not sure why. Is there a better way to do this?
The initial for(var r... statement runs within another loop drawing on a canvas, so I run the loops this way to keep from updating the DOM every second or more.
I would prefer not putting the startTime into a separate array or anything like that as I would need to take that array and refer to the original json anyway and it just seems like a waste.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您基本上找到了第一个符合您的测试条件的节点。您需要找到的是大于当前步骤的最低步骤,因此您应该只更新
nextStep
,存储nt
和nst
,然后在循环外运行命令。像这样:请注意,如果两个开始时间相同,它将返回它在该开始时间找到的第一个任务。
You're basically finding the first node that qualifies under your test. What you need to find is the lowest one that's greater than your current step, so you should just update
nextStep
, storent
andnst
and then run your commands outside the loop. Like so:Note that if two start times are the same it will return the first task it finds with that start time.