Home > OS >  How to get data from set of array between two json?
How to get data from set of array between two json?

Time:11-01

I have two json file, One is Courses and another one is instructor. Courses and instructor are matched with _id. Now what I want is when an instructor details is send, all courses of that particular instructor is also send in the response. How may I suppose to do that?

Instructor JSON data

let instructorData = [
  {
    _id: 1,
    name: "superman",
    courses: [1,3],
  },{
    _id: 2,
    name: "batman",
    courses: [2],
  }
];

module.exports = {
    instructorData: instructorData,
  };
  

Courses JSON data

let coursesData = [
  {
    _id: 1,
    name: "DC",
    instructor: {
                _id:1
                 },
  },{
    _id: 2,
    name: "Marvel",
    instructor: {
                _id:2
                 },
  },{
    _id: 3,
    name: "DC vs Marvel",
    instructor: {
                _id:1
                 },
  },
];

module.exports = {
    coursesData: coursesData,
  };
  

for Instructor 1 here I have 2 courses. How may I find/iterate the 2 courses?

For example when I am getting an instructor I want it like:

{ name: "superman",
 courses: [{name: "DC"},{name: "DC vs Marvel"}] 
} 

CodePudding user response:

hope this help.

let instructorData = [
    {
        _id: 1,
        name: "superman",
        courses: [1, 3],
    },
    {
        _id: 2,
        name: "batman",
        courses: [2],
    }
];

let coursesData = [
    {
        _id: 1,
        name: "DC",
        instructor: {
            _id: 1
        },
    },
    {
        _id: 2,
        name: "Marvel",
        instructor: {
            _id: 2
        },
    },
    {
        _id: 3,
        name: "DC vs Marvel",
        instructor: {
            _id: 1
        },
    },
];

const getInstructor = id => {
    const found = instructorData.find(instructor => instructor._id == id);
    if (!found) return "Instructor not found!";
    found.courses = found.courses.map(course => coursesData.find(courseData => courseData._id === course));
    return found;
}

console.log(getInstructor(1));
console.log(getInstructor(2));
console.log(getInstructor(3));

CodePudding user response:

I don't think you will run into not found errors this way

let instructorData = [
  {
    _id: 1,
    name: "superman",
    courses: [1,3],
  },{
    _id: 2,
    name: "batman",
    courses: [2],
  }
];


let coursesData = [
  {
    _id: 1,
    name: "DC",
    instructor: {
                _id:1
                 },
  },{
    _id: 2,
    name: "Marvel",
    instructor: {
                _id:2
                 },
  },{
    _id: 3,
    name: "DC vs Marvel",
    instructor: {
                _id:1
                 },
  },
];




let instructorCourses = [];
instructorData.forEach((instructor) => {
    let instructorId = instructor._id;
    let instructorCourse = {};
    instructorCourse.instructorId = instructorId;
    instructorCourse.instructorName = instructor.name;
    instructorCourse.courses = [];
    coursesData.forEach((courseD) => {
        let courseInstructorId = courseD.instructor._id;
        if (instructorId === courseInstructorId) {
            let course = {};
            course.courseId = courseD._id;
            course.courseName = courseD.name;
            instructorCourse.courses.push(course);
        }

    });
    instructorCourses.push(instructorCourse);
});

let value = JSON.stringify(instructorCourses, null, "\t");
console.log(value);

  • Related