Javascript 跳到下一课或下一模块的第一节

Javascript 跳到下一课或下一模块的第一节,javascript,Javascript,我在创建一个脚本以进入课程的下一课时遇到问题 我的结构是这样的 const课程={ 标题:“课程名称”, 持续时间:“1小时30分钟”, 模块:[ { id:1, 标题:“第一个模块”, 经验教训:[ { id:1, 模块ID:1, 标题:“第1课”, 视频:'https://video.com/any.mp4', }, { id:2, 模块ID:1, 标题:“第二课”, 视频:'https://video.com/any.mp4', }, ], }, { id:2, 标题:“第二模块”, 经验

我在创建一个脚本以进入课程的下一课时遇到问题

我的结构是这样的

const课程={
标题:“课程名称”,
持续时间:“1小时30分钟”,
模块:[
{
id:1,
标题:“第一个模块”,
经验教训:[
{
id:1,
模块ID:1,
标题:“第1课”,
视频:'https://video.com/any.mp4',
},
{
id:2,
模块ID:1,
标题:“第二课”,
视频:'https://video.com/any.mp4',
},
],
},
{
id:2,
标题:“第二模块”,
经验教训:[
{
id:1,
模块ID:2,
标题:“第1课”,
视频:'https://video.com/any.mp4',
},
{
id:2,
模块ID:2,
标题:“第二课”,
视频:'https://video.com/any.mp4',
},
],
}
],
}
当课程视频结束时,挂钩处于活动状态,必须执行脚本,但我在创建此脚本时遇到困难,业务规则是:转到下一课,如果没有,转到第一课下一个模块,如果没有下一课模块,则什么也不做

我有用户当前使用的反应数据

const currentModule={
id:1,
标题:“第一个模块”,
}
常数当前课程={
id:2,
模块ID:1,
标题:“第二课”,
视频:'https://video.com/any.mp4',
}
我需要填写以下内容:

让nextLesson={}
设nextModule={}
我可以尝试什么来实现这一点?

尝试以下方法:

let nextModule = {};
let nextLesson = {};

// Keep track of the module you are in
const currentModuleIndex = course.modules.map((module) => module.id).indexOf(currentModule.id);

// Keep track of the lesson you are in
const _currentModule = course.modules.filter((module) => module.id === currentModule.id)[0];
const currentLessonIndex = _currentModule.lessons.map((lesson) => lesson.id).indexOf(currentLesson.id)

// If you are at the end lesson of a module and there is no other modules left
if (course.modules.length === currentModuleIndex + 1) {
  console.log('you are at the end lesson');
} else
// If the current lesson is the end of the lessons in the current module
if (course.modules[currentModuleIndex].lessons.length === currentLessonIndex + 1) {
  // next module will be +1 the module you were tracking
  nextModule = {
    id: course.modules[currentModuleIndex + 1].id,
    title: course.modules[currentModuleIndex + 1].title
  }

  // next lesson is just the data from the first lesson of that module
  nextLesson = {
    id: course.modules[currentModuleIndex + 1].lessons[0].id,
    moduleId: course.modules[currentModuleIndex + 1].id,
    title: course.modules[currentModuleIndex + 1].lessons[0].title,
    video: course.modules[currentModuleIndex + 1].lessons[0].video,
  }
} else {
  // next module is the same as the current
  nextModule = currentModule;

  // next lesson will be +1 to the lesson you were tracking
  nextLesson = {
    id: course.modules[currentModuleIndex].lessons[currentLessonIndex + 1].id,
    moduleId: course.modules[currentModuleIndex].id,
    title: course.modules[currentModuleIndex].lessons[currentLessonIndex + 1].title,
    video: course.modules[currentModuleIndex].lessons[currentLessonIndex + 1].video,
  }
}

这应该行得通。它只是计算出当前模块和课程的索引,然后检查是否有下一个。(您可能会对其进行重构以使其更整洁。)

const课程={
标题:“课程名称”,
持续时间:“1小时30分钟”,
模块:[{id:1,标题:'first module',课程:[{id:1,模块id:1,标题:'lesson 1',视频:'https://video.com/any.mp4},{id:2,moduleId:1,标题:'lesson 2',视频:'https://video.com/any.mp4“}]},{id:2,标题:“第二个模块”,课程:[{id:1,模块id:2,标题:“课程1”,视频:'https://video.com/any.mp4'},{id:2,模块id:2,标题:“第2课”,视频:'https://video.com/any.mp4'}]}]}
常数当前课程={
id:2,
模块ID:1,
标题:“第二课”,
视频:'https://video.com/any.mp4',
}
常量getNextModule=({id:currentLessonId,moduleId:currentModuleId})=>{
让下一个模块;
让nextLesson;
课程.modules.forEach((模块,当前模块索引)=>{
if(module.id==currentModuleId){
//查找当前模块当前课程的索引
const currentLessonIndex=module.lessons.findIndex(lesson=>lesson.id==currentLessonId);
如果(!模块课程[currentLessonIndex+1]){
//当前模块没有下一课,请学习下一课
//第一课呢
nextModule={…course.modules[currentModuleIndex+1]};
nextLesson={…nextModule.lessons[0]};
返回;
}
//学习当前模块和下一课
nextModule={…module};
nextLesson={…nextModule.lessons[currentLessonIndex+1]};
}
})
返回{nextModule,nextLesson};
}
log(getNextModule(currentLesson));