[
{
id: 1,
step: 1,
key: "q_13",
question: { en: "How are you?" },
answer: {
type: "single",
subtype: "list",
values: {
1: { en: "I'm okay" },
2: { en: "Bad" }
}
}
},
{
id: 2,
key: "q_14",
step: 1,
question: { en: "How old are you?" },
answer: {
type: "single",
subtype: "list",
values: {
1: { en: "22" },
2: { en: "23" },
3: { en: "35" }
}
}
},
{
id: 3,
step: 2,
key: "q_15",
question: { en: "Your height?" },
answer: {
max: 100,
min: 0,
subtype: "range",
type: "single"
}
}
];
{
"q_15" : {step: 2, answer: "62"},
"q_14" : {step: 1, answer: "2"}, // то есть ответ был 23
"q_13" : {step: 1, answer: "1"} // ответ I'm okay
}
[
// Ключи - это цифра step, потому как мне надо отображать вопросы и ответы согласно step'у в котором он был
1: [
{step: 1, question: { en: "How are you?" }, answer: "I'm okay", },
{step: 1, question: {en: "How old are you?"}, answer: "23" }
],
2: [
{step: 2, question: { en: "Your height?" }, answer: "62"}
]
]
const steps = Object.entries(answers).reduce((acc, [ key, val ]) => {
const question = questions.find(n => n.key === key);
const step = acc[val.step] = acc[val.step] || [];
step.push({
step: val.step,
question: { ...question.question },
answer: question.answer.values instanceof Object
? question.answer.values[val.answer].en
: val.answer,
});
return acc;
}, {});