0

Based on the following post: Rendering a dynamic survey

let questions = [{idquestion: 1, question:"age?"}, {idquestion: 2, question: "sex?"}];
let answers = [{idanswer: 1, answer:"17", idquestion: 1}, {idanswer: 2, question: "male", idquestion: 2}];
let questionanswer = {question: 'edad?', answer: '17'};
let questionsAndAnswers =
    questions.map(question => {question:question, answer: answers.find((answer) => answer.idquestion === question.idquestion)})

I obtain the following errors:

question: unused label

answer: cannot find name answer

I want to obtain an object questionAndAnswers with questions and answers related. How can I fix it?

Community
  • 1
  • 1

1 Answers1

1
let questionsAndAnswers = questions.map( (question) => {
    return { question: question, answer: answers.filter( (answer) => { return answer.idquestion === question.idquestion; })[0]}
});

BTW Functional coding style is interesting but this method feels pretty dirty.

http://codepen.io/amishstripclub/pen/wzbYoZ?editors=0010

Eric N
  • 2,136
  • 13
  • 13
  • I think it feels a lot less dirty with a little judicious formatting (and use of the [shorthand object return syntax](http://stackoverflow.com/questions/28770415/ecmascript6-arrow-function-that-returns-an-object)): http://codepen.io/anon/pen/jroZyk?editors=0010 – Joe Clay Oct 28 '16 at 14:51
  • Yeah, that is better. I might refactor the answers function out to a separate function named "mapAnswerstoQuestions" or something of the sort. Then I could get the gist of what was being assigned to answers without having to mentally parse the logic out. – Eric N Oct 28 '16 at 14:54