Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
I want to return the groups and the names of the subjects in which they are enrolled
const groups = [
  {
    name: "1",
    subjects: [1, 2]
  },
   {
    name: "2",
    subjects: [1]
  },
]

const subjects = [
   {
    id: 1,
    name: "English",
  },
  {
    id: 2,
    name: "Mathematics",
  },
  {
    id: 3,
    name: "Physics",
  },
]

example:[
  {
    name: "1",
    subjects: [
   {
    id: 1,
    name: "English",
  },
  {
    id: 2,
    name: "Mathematics",
  },
]

   const groupsSubject = groups.map(group => {
return {
...group,
subjects: subjects.id

}
})
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can create an object lookup of subjects based on the id. Then iterate through your group and assign the respective subjects.

const groups = [ { name: "1", subjects: [1, 2] }, { name: "2", subjects: [1] }],
      subjects = [ { id: 1, name: "English", }, { id: 2, name: "Mathematics", }, { id: 3, name: "Physics", }, ],
      lookup = Object.fromEntries(subjects.map(o => [o.id, o])),
      result = groups.map(({name, subjects}) => ({name, subjects: subjects.map(id => ({...lookup[id]}))}));
console.log(result);
.as-console-wrapper { min-height: 100%!important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

This is my answer:

const groups = [
  {
    name: '1',
    subjects: [ 1, 2 ]
  },
  {
    name: '2',
    subjects: [ 1 ]
  }
]

const subjects = [
  {
    id: 1,
    name: 'English'
  },
  {
    id: 2,
    name: 'Mathematics'
  },
  {
    id: 3,
    name: 'Physics'
  }
]

const group = (groups, subjects) => {
  return groups.map((group) => ({
    name: group.name,
    subjects: subjects.filter((subject) => group.subjects.includes(subject.id))
  }))
}

console.log(group(groups, subjects))

Output:

[
  { name: '1', subjects: [ [Object], [Object] ] },
  { name: '2', subjects: [ [Object] ] }
]
about 4 years ago · Juan Pablo Isaza Report

0

  • Using Map and Array#map, create a map where the subject id is the key and the subject is the value
  • Using Array#map, iterate over the groups array. In each iteration, the subjects list will be created using Array#map and Map#get to transform ids to objects

const _getGroupWithSubjectDetails = (groups = [], subjects = []) => {
  const subjectMap = new Map(
    subjects.map(subject => ([subject.id, subject]))
  );
  return groups.map(({ subjects = [], ...group }) => ({ 
    ...group, 
    subjects: subjects.map(subjectId => ({ ...(subjectMap.get(subjectId) || {}) }))
  }));
}

const 
  groups = [ { name: "1", subjects: [1, 2] }, { name: "2", subjects: [1] } ],
  subjects = [ { id: 1, name: "English" }, { id: 2, name: "Mathematics" }, { id: 3, name: "Physics" } ];

console.log( _getGroupWithSubjectDetails(groups, subjects) );

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!