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

97
Visualizações
Merge Object array based on id

Hi I am calling an API where it returns an Object containing questions and answers like so

{
   "questions":[
      {
         "id":1,
         "questionHeader":"Some question header",
         "questionText":"Some question text?",
      },
      {
         "id":2,
         "questionHeader":"Some question header",
         "questionText":"Some question text?",
      },
   ],
   "answers":[
      {
         "id":1,
         "questionId":1,
         "answer":"Some answer",
      },
      {
         "id":2,
         "questionId":1,
         "answer":"Some answer",
      },
      {
         "id":3,
         "questionId":2,
         "answer":"Some answer",
      },
   ]
}

What I am trying to achieve is to get the answers embedded within their respective question based on a questions id and an answers questionId. So for the above, I am trying to achieve something like the following.

{
   "questions":[
      {
         "id":1,
         "questionHeader":"Some question header",
         "questionText":"Some question text?",
         "answers":[
          {
             "id":1,
             "questionId":1,
             "answer":"Some answer",
          },
          {
             "id":2,
             "questionId":1,
             "answer":"Some answer",
          },
        ]
      },
      {
         "id":2,
         "questionHeader":"Some question header",
         "questionText":"Some question text?",
         "answers":[
          {
            "id":3,
            "questionId":2,
            "answer":"Some answer",
          },
        ]
      },
   ],
}

I have something which adds some answers but not as an array of objects per question. Currently I have this

const newItem = data.questions.map((t1) => ({
    ...t1,
    ...data.answers.find((t2) => t2.questionId === t1.id),
}));
console.log(newItem);

I have set up a JSFiddle. How can I achieve my desired output?

Thanks

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Index the questions by ID first, then iterate over the answers. The question ID will make it easy to access the associated question - just use bracket notation to look it up on the indexed object, and you can push to that question's answers. After iteration is finished, turn the questions back into an array again.

const data={questions:[{id:1,questionHeader:"Some question header",questionText:"Some question text?"},{id:2,questionHeader:"Some question header",questionText:"Some question text?"}],answers:[{id:1,questionId:1,answer:"Some answer"},{id:2,questionId:1,answer:"Some answer"},{id:3,questionId:2,answer:"Some answer"}]};

const questionsById = Object.fromEntries(
  data.questions.map(question => [question.id, { ...question, answers: [] }])
);
for (const answer of data.answers) {
  questionsById[answer.questionId].answers.push(answer);
}
const output = Object.values(questionsById);
console.log(output);

The above is O(n) because there are no nested loops. If you wanted to tweak your nested loop approach (O(n ^ 2)) so that it works. .filter instead of .find so you can get an array of answers, and prefix the property in the object there with answers: instead of spreading.

const data={questions:[{id:1,questionHeader:"Some question header",questionText:"Some question text?"},{id:2,questionHeader:"Some question header",questionText:"Some question text?"}],answers:[{id:1,questionId:1,answer:"Some answer"},{id:2,questionId:1,answer:"Some answer"},{id:3,questionId:2,answer:"Some answer"}]};

const newItem = data.questions.map((t1) => ({
    ...t1,
    answers: data.answers.filter((t2) => t2.questionId === t1.id),
}));
console.log(newItem);

about 4 years ago · Santiago Trujillo Relatório

0

Array#filter can be used to find all the answers with a specific question id. (Note that Array#find only returns the first element matching a condition.)

const data={questions:[{id:1,questionHeader:"Some question header",questionText:"Some question text?"},{id:2,questionHeader:"Some question header",questionText:"Some question text?"},],answers:[{id:1,questionId:1,answer:"Some answer"},{id:2,questionId:1,answer:"Some answer"},{id:3,questionId:2,answer:"Some answer"},]};
const newItem = data.questions.map((t1) => ({
    ...t1,
    answers: data.answers.filter((t2) => t2.questionId === t1.id),
}));
console.log(newItem);

about 4 years ago · Santiago Trujillo 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