0

I have this course structure in hand, but I am not able to figure out how I can fetch a lesson object by ID and as well as how I can update a lesson object with modified property values?

If anyone could shine some light to the right path will be much appreciated.

{
   id: 1,
   name: 'An Amazing Course',
   description: 'Mauris ac efficitur enim, nec commodo quam. Nunc vehicula blandit porta.',
   sections: [
      {
         id: 1,
         sort_order: 1,
         name: 'Section one',
         lessons: [
            {
               id: 1,
               sort_order: 1,
               name: 'Lesson One'
            },
            {
               id: 3,
               sort_order: 2,
               name: 'Lesson Three'
            },
            {
               id: 2,
               sort_order: 3,
               name: 'Lesson Two'
            }
         ]

      },
      {
         id: 2,
         sort_order: 2,
         name: 'Section Two',
         lessons: [
            {
               id: 4,
               sort_order: 1,
               name: 'Lesson Four'
            },
            {
               id: 5,
               sort_order: 2,
               name: 'Lesson Five'
            },
            {
               id: 6,
               sort_order: 3,
               name: 'Lesson Six'
            }
         ]
      }
   ]
}

2 Answers2

1
const obj = {
   id: 1,
   name: 'An Amazing Course',
   description: 'Mauris ac efficitur enim, nec commodo quam. Nunc vehicula blandit porta.',
   sections: [
      {
         id: 1,
         sort_order: 1,
         name: 'Section one',
         lessons: [
            {
               id: 1,
               sort_order: 1,
               name: 'Lesson One'
            },
            {
               id: 3,
               sort_order: 2,
               name: 'Lesson Three'
            },
            {
               id: 2,
               sort_order: 3,
               name: 'Lesson Two'
            }
         ]

      },
      {
         id: 2,
         sort_order: 2,
         name: 'Section Two',
         lessons: [
            {
               id: 4,
               sort_order: 1,
               name: 'Lesson Four'
            },
            {
               id: 5,
               sort_order: 2,
               name: 'Lesson Five'
            },
            {
               id: 6,
               sort_order: 3,
               name: 'Lesson Six'
            }
         ]
      }
   ]
}

const lessons = [].concat.apply([], Object.values(obj.sections).map(section => section.lessons))

const id = //whichever

const selected = lessons.find(lesson => lesson.id === id)

You can try with something like this, flattening the object for having all lessons in a plain array. In this way you can use find/filter without problem.

With a little more complex logic you can maintain the id references of the course and the lesson in the flat lesson array

[].concat.apply([], Object.values(obj.sections).map(section => 
  section.lessons.map(lesson => ({
    ...lesson,
    courseId: obj.id,
    sectionId: section.id
  }))
))
Al Hill
  • 479
  • 2
  • 6
  • Thank you so much for the quick response. When you flatten the array doesn't it loose the reference to the original object called `obj` while updating the property of the lesson object? – K. Reynolds Jun 08 '20 at 18:54
  • In the way I have done it, yes, you lose the reference. But you can extend the return of the inner map to add this reference – Al Hill Jun 08 '20 at 18:56
  • I have added a little more complex snippet to maintain the parent references – Al Hill Jun 08 '20 at 19:03
  • This is exactly I am after. Thanks @al-hill – K. Reynolds Jun 08 '20 at 19:10
0

This to search lesson by id

function findless(ids){
           let  result
         const lesvalues=Object.values(les)

          lesvalues.filter(x=>Array.isArray(x)?x.forEach(y=>y.lessons.find(x=>x.id==ids?result=x.name:null)):null)
          return result
         }
         console.log(findless(3))
Sven.hig
  • 4,449
  • 2
  • 8
  • 18