I have a list of courses in below json. I have tried to get the desired output to make the array as I want but I am getting the isssues in making the new array.
I am trying to get it using the below snippet but it is showing error for using [courses[i].client_id] with the result.
let courses = [
{course_id: 1, name: "Test1", description: "Test1 desc", video_url: "111", client_id: 1},
{course_id: 3, name: "A new course", description: "This is a temporary description used to test functionality. ", video_url: "https://vimeo.com/", client_id: 1},
{course_id: 5, name: "Teaching", description: "programming language", video_url: "https://vimeo.com", client_id: 1},
{course_id: 13, name: "Course285", description: "desc285", video_url: "", client_id: 2},
{course_id: 16, name: "The NEW", description: "The Course is an training", video_url: "https://vimeo.com/", client_id: 1}
]
let result = [];
for (let i = 0; i < courses.length; i++) {
if (courses && courses.length) {
result[i][courses[i].client_id] = {
clientId: courses[i].client_id
} ;
}
}
Expected Output
[
{client_id: 1, courses:[{
course_id:1,
course_name: Test1,
course_desc: description
},{
course_id:2,
course_name: Test1,
course_desc: description
}]},
{client_id:2,courses[{
....
}]}
]
coursesis neither valid JSON nor JavaScript. Consider posting an actual working example.