0

I have a result json like this

[
       {"courseStarted" :[
               {"CategoryName":"CategoryName","courseName" :"Equipment Course", "seriesName": "Free Education", "courseDetails" :[
                       {"totalLessons" :"2", "NoLessonComplete":"1", "lastviewed":"date", "lessonDetails":[
                               {"lessonId":"342367","quiz":"quizReady"},
                               {"lessonId" :"345345", "quiz":"quizReady"},
                               {"lessonId":"435435","quiz":"quizReady"},
                               {"lessonId":"4234234", "quiz":"quizFailed"},
                               {"lessonId":"4234234","quiz":"quizPassed"}
                       ]}
               ]}
       ]},
       {"courseStarted" :[
               {"CategoryName":"CategoryName","courseName" :"Equipment Course1", "seriesName": "Free Education1", "courseDetails" :[
                       {"totalLessons" :"21", "NoLessonComplete":"11", "lastviewed":"date1", "lessonDetails":[
                               {"lessonId":"3423671","quiz":"quizReady"},
                               {"lessonId" :"3453451", "quiz":"quizReady"},
                               {"lessonId":"4354351","quiz":"quizReady"},
                               {"lessonId":"42342341", "quiz":"quizReady"},
                               {"lessonId":"42342341","quiz":"quizPassed"}
                       ]}
               ]}
       ]}

    ]

from this json output how can i access the value of lessonId using the $.each function.

1

1 Answer 1

1
$.each(data, function(){

    // Print course name
    console.log(this.courseStarted[0].courseName);

    $.each(this.courseStarted[0].courseDetails[0].lessonDetails, function(){
        // Print lesson id
        console.log(this.lessonId);
    });

});
Sign up to request clarification or add additional context in comments.

2 Comments

Yah it is working. But how could it getting the value by calling 0th element of course started. It should be incremented like for first this.courseStarted[0].courseName and for second this.courseStarted[1].courseName like that. I got it working but i did'nt get the workflow, i mean i did'nt getting how it's working. Can you tell me the theory behind this.
@user359187 If you look at courseStarted in your object it is always an array with exactly one value. That value is an object which has a courseName. In the code you posted you have two course names. data[0].courseStarted[0].courseName and data[1].courseStarted[0].courseName. The $.each handles the iteration through the data array. Does that make sense now?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.