-2

I'm building a quiz app and I have 2 arrays of objects. I want to obtain another array associating the answers to their question.

How can I do this?

const questions = [{
  id: 1,
  text: question1
}, {
  id: 2,
  text: question2
}, {
  id: 3,
  text: question3
}]

const answers = [{
  id: 1,
  text: answer1,
  questions_id: 1
}, {
  id: 2,
  text: answer2,
  questions_id: 1
}, {
  id: 3,
  text: answer3,
  questions_id: 1
}, {
  id: 4,
  text: answer4,
  questions_id: 1
}...]

I need to get an array with the answers associated to each questions. I know I should use map or filter functions, but I've never done it with 2 different arrays comparing one to another. Can anyone help me? thank you. I tried this but doesn't work, it return an array of undef:

let answerQuestionId = questions.map((q,a) => {
let temp = answers.find(element => element.domanda_id === q.id)

});

  • you should prepare a working snippet how you tried to solve this problem, otherwise it's asking doing homework for you – Alexander Nenashev Jun 26 '23 at 16:12
  • @AlexanderNenashev sorry I edited, is it okay now? – Fabrizio Mastrone Jun 26 '23 at 16:21
  • The reason you are getting undefined right now is because your map function does not return anything. You declare a variable `temp` and do nothing with it after that. However I will add that I don't think `Array.map()` is going to do what you think it's going to do. You might have more luck looking into an actual `Map` – Portal Jun 26 '23 at 16:23
  • Now I tried `let answerQuestionId = questions.map((q,a) => { let temp = answers.filter(element => `${element.domanda_id}` === `${q.id}`) return temp; }); console.log(answerQuestionId)` And i get multiple arrays of answers sorted by the question_id – Fabrizio Mastrone Jun 26 '23 at 16:35

2 Answers2

0

This is how you could combine both arrays into a single one with the corresponding question to each answer using Array#map and Array#find:

const questions = [ { id: 1, text: "question1" }, { id: 2, text: "question2" }, { id: 3, text: "question3" }];

const answers = [ { id: 1, text: "answer1", questions_id: 1 }, { id: 2, text: "answer2", questions_id: 1 }, { id: 3, text: "answer3", questions_id: 1 }, { id: 4, text: "answer4", questions_id: 1 }];

const result = answers.map(a => ({
  ...a,
  question: {...questions.find(q => q.id === a.questions_id)}
}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Behemoth
  • 5,389
  • 4
  • 16
  • 40
  • I tried and it returns me the same array of objects as the one of the answers – Fabrizio Mastrone Jun 26 '23 at 16:45
  • Each object has a property `question` which is the corresponding question to each answer. If that's not what you want please edit your question and show me your expected result :) – Behemoth Jun 26 '23 at 16:48
0

you can use filter mathod to get associated answer with the questions.

const questions = [
  {
    id: 1,
    text: "question1"
  },
  {
    id: 2,
    text: "question2"
  }, 
  {
    id: 3,
    text: "question3"
  }
]

const answers = [
  {
    id: 1,
    text: "answer1",
    questions_id: 1
  }, 
  {
    id: 2,
    text: "answer2",
    questions_id: 1
  }, 
  {
    id: 3,
    text: "answer3",
    questions_id: 1
  }, 
  {
    id: 4,
    text: "answer4",
    questions_id: 1
  }
]

const data = questions.map((que) => {
  return {
    ...que,
    answer: answers.filter((ans) => ans.questions_id === que.id)
  }
})

console.log("data is ================" , data);
sadeq shahmoradi
  • 1,395
  • 1
  • 6
  • 22
nirali
  • 11
  • 3