Arnaldo Antonio Torres Arnaldo Antonio Torres - 1 year ago 115
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 = => {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
with questions and answers related. How can I fix it?

Answer Source
let questionsAndAnswers = (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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download