-2

var course = ["HTML", "CSS", "JAVASCRIPT", "PYTHON"]

var student = { name: "john doe", course } { name: "gary lary", course }

I want the output to be group1 [html css javascript python] john doe is assigned html gary lary css and so on

yerin
  • 1
  • 2
    Please use correct JavaScript syntax and provide full desired output – Konrad Sep 27 '22 at 18:01
  • this is wrong syntax `var student = [{ name: "john doe", course },{ name: "gary lary", course }]`. it is an array with 2 objects – UmairFarooq Sep 27 '22 at 18:02
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 27 '22 at 20:57

1 Answers1

1

It depends on how you want to combine them. If you don't want to randomize courses to students, you can just match the index of the student with the course. You can use the map array method:

let courses = ["HTML", "CSS", "JAVASCRIPT", "PYTHON"]
let students = [ {name: "john doe" },{ name: "gary lary"}]

const studendsWithCourses = students.map((student, index) => ({...student, course: courses[index]}))

Note that in this case you'll get an error if the array if students is bigger then the array of courses. You can fix this by changing the map to this:

const studendsWithCourses2 = students.map((student, index) => ({...student, course: courses[index % courses.length]})).

This way, courses will be distributed in order to students and you won't get an error if the students array is bigger than the courses array.