I am trying to get data from a JS object by using array.map() function. However, the output I am getting is null. I am not able to understand the problem here.
This is the object
export const Course = [
{
course_id: "001-TTE-ADIT",
course_description:"XXXX",
course_title: "XXXX",
course_faculty: "XXXX",
course_duration: "XXXX",
course_lectures_count: "XXXX",
course_fees: XXXX,
course_section: [
{
section_id: "001-TTE-ADIT-S01",
section_name: "XXXX",
section_description:
"TXXXX.",
section_duration: 3,
section_lecture_count: 3,
section_lectures: [
{
lecture_id: "001-TTE-ADIT-S01-LE01",
lecture_name: "XXXX",
lecture_description:
"XXXX.",
lecture_faculty: "XXXX",
lecture_duration: 1,
lecture_link: "#",
lecture_notes: [
{
note_title: "XXXX",
note_link: "#",
},
{
note_title: "XXXX",
note_link: "#",
},
{
note_title: "XXXX",
note_link: "#",
},
{
note_title: "XXXX",
note_link: "#",
},
],
},
{
lecture_id: "001-TTE-ADIT-S01-LE02",
lecture_name: "XXXX",
lecture_description:
"XXXX.",
lecture_faculty: "XXXX",
lecture_duration: 0.5,
lecture_link: "#",
lecture_notes: [
{
note_title: "XXXX",
note_link: "#",
},
],
},
],
},
{
section_id: "001-TTE-ADIT-S02",
section_name: "XXXX",
section_description:
"XXXX",
section_duration: 5,
section_lecture_count: 5,
section_lectures: [
{
lecture_id: "001-TTE-ADIT-S00-LE03",
lecture_name: "XXXX",
lecture_description:
"XXXX",
lecture_faculty: "XXXX",
lecture_duration: 1,
lecture_link: "#",
lecture_notes: [
{
note_title: "XXXX",
note_link: "#",
},
{
note_title: "XXXX",
note_link: "#",
},
{
note_title: "XXXX",
note_link: "#",
},
{
note_title: "XXXX",
note_link: "#",
},
],
},
{
lecture_id: "001-TTE-ADIT-S00-LE04",
lecture_name: "XXXX",
lecture_description:
"XXXX",
lecture_faculty: "XXXX",
lecture_duration: 1,
lecture_link: "#",
lecture_notes: [
{
note_title: "XXXX",
note_link: "#",
},
],
},
{
lecture_id: "001-TTE-ADIT-S00-LE05",
lecture_name: "XXXX",
lecture_description:
"XXXX",
lecture_faculty: "XXXX",
lecture_duration: 1,
lecture_link: "#",
lecture_notes: [
{
note_title: "XXXX",
note_link: "#",
},
{
note_title: "XXXX",
note_link: "#",
},
{
note_title: "XXXX",
note_link: "#",
},
{
note_title: "XXXX",
note_link: "#",
},
],
},
],
},
],
},
];
And this is the function for getting data from object
import { Course } from "../../Data/Courses";
export const getListLectures = async () => {
let lecture_list = [];
lecture_list = Course.map((course) => {
course.course_section?.map((section) => {
section.section_lectures?.map((lecture) => {
lecture_list.push(lecture?.lecture_name);
});
});
});
return lecture_list;
};
The expected outcome from this function was list of lectures which lie within each section for the given course.
2
Answers
First of all you are not returning returning from
Course.map()
. Thats why it is returning undefined,The other problem is that you are assigning
Array.map
‘s result tolecture_list
and also pushing the results in the same arrayIf you are not returning from
Array.map
, better to useArray.forEach
or other loopsYou can use a mix of
map
andflatMap
– making sure you return each mapped array each time.For the purposes of this example:
Course
tocourses
to better match the data descriptioncourse_fees
to zero