Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

149
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda