0

Array Structures

Hi, I'm building an app that shows students which courses they qualify for based on their grades. I have to filter courses that have the same subjects as the student's subjects and the mark must be>= value. I've attached an image of how the JSON looks like, the first 6 objects are the student's marks, the other 10 objects are the courses, each course has an array of subjects... these are the subjects we're checking.

Nick Parsons
51.6k6 gold badges61 silver badges80 bronze badges
asked Feb 3, 2020 at 1:53
1
  • Post the JavaScript (like a function -- not just an object) code as a minimal reproducible example. The image you have posted is not very helpful. Commented Feb 3, 2020 at 2:07

2 Answers 2

1

For the student array, you can create a mark_lookup Map which points each Subject key to a Mark value. Then, you can use .filter() on your courses array to find all courses where .every() subject from the inner Subjects array has a mark_lookup value greater than or equal to the value for that particular subject.

See example below:

const student = [{Subject: "math", Mark: 75}, {Subject: "science", Mark: 45}, {Subject: "programming", Mark: 90}];
const courses = [{ Name: "Computer Science", Subjects: [{name: "math", value: 50}, {name: "programming", value: 60}] }, { Name: "Information Technology", Subjects: [{name: "programming", value: 50}] }, { Name: "Chemistry", Subjects: [{name: "math", value: 50}, {name: "science", value: 50}] }, { Name: "Psychology", Subjects: [{name: "english", value: 50}, {name: "science", value: 50}] }];
const getCourses = (student, courses) => {
 const mark_lookup = new Map(student.map(({Subject, Mark}) => [Subject, Mark]));
 return courses.filter(({Subjects}) => Subjects.every(
 ({name, value}) => (mark_lookup.get(name) || 0) >= value
 ));
}
console.log(getCourses(student, courses));
.as-console-wrapper { max-height: 100% !important;} /* ignore */

answered Feb 3, 2020 at 2:11

Comments

0

1) Create student marks object from student array. 2) Go thru course, and each course filter the subjects bases on the requirement. 3) push course with updated subjects to results if subjects are more than 0

const student = [
 { Subject: "English First Additional Language", Mark: 80 },
 { Subject: "Physical Sciences", Mark: 60 },
 { Subject: "Mathematics", Mark: 70 }
];
const courses = [
 {
 Name: "Computer Science",
 Subjects: [
 { name: "English First Additional Language", value: 50 },
 { name: "Mathematics", value: 80 }
 ]
 },
 {
 Name: "Information Technology",
 Subjects: [{ name: "Physical Sciences", value: 50 }]
 },
 {
 Name: "Chemistry",
 Subjects: [{ name: "Physical Sciences", value: 70 }]
 }
];
const marks = student.reduce((acc, { Subject, Mark }) => {
 acc[Subject] = Mark;
 return acc;
}, {});
const short_listed = courses.reduce((acc, { Name, Subjects }) => {
 const sel_subjects = Subjects.filter(({ name, value }) => marks[name] > value);
 if (sel_subjects.length > 0) {
 acc.push({
 Name,
 Subjects: sel_subjects
 });
 }
 return acc;
}, []);
console.log(short_listed); 

answered Feb 3, 2020 at 4:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.