У меня есть массив с объектом внутри, который содержит больше объектов, которые я хотел бы отобразить на более простую структуру.
Я пробовал следующее, и кажется, что циклически перебирает объекты и возвращает одинобъект для каждого цикла без данных.
Object.values(data).forEach((a) => {
console.log({
managingRole: a.id === 110 ? a.answer : null,
advice: a.id === 112 ? a.answer : null,
});
});
Конечная цель - вернуть массив объектов для сопоставления, например:
const desired = [{
managingRole: 'spending',
advice: 'everyone'
},
{
managingRole: 'saving',
advice: 'no one'
}];
Ниже приведены данные, с которыми я работаю:
const data = [{
'110':
{
id: 110,
type: 'RADIO',
question: '<strong>My main role in managing my money is:</strong>',
section_id: 9,
answer: 'spending'
},
'111':
{
id: 111,
type: 'RADIO',
question: '<strong>When it comes to financial matters, I most agree with which statement?</strong>',
section_id: 9,
answer: 'spend it'
},
'112':
{
id: 112,
type: 'RADIO',
question: '<strong>When deciding on an investment, I trust the advice of :</strong>',
section_id: 9,
answer: 'everyone'
}
},
{
'110':
{
id: 110,
type: 'RADIO',
question: '<strong>My main role in managing my money is:</strong>',
section_id: 9,
answer: 'saving'
},
'111':
{
id: 111,
type: 'RADIO',
question: '<strong>When it comes to financial matters, I most agree with which statement?</strong>',
section_id: 9,
answer: 'save it'
},
'112':
{
id: 112,
type: 'RADIO',
question: '<strong>When deciding on an investment, I trust the advice of :</strong>',
section_id: 9,
answer: 'no one'
}
}];