Arnaldo Antonio Torres Arnaldo Antonio Torres - 1 month ago 18
TypeScript Question

Mapping answers to questions

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?

Answer
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

Comments