Карта объектов внутри объекта в массиве - PullRequest
0 голосов
/ 14 декабря 2018

У меня есть массив с объектом внутри, который содержит больше объектов, которые я хотел бы отобразить на более простую структуру.

Я пробовал следующее, и кажется, что циклически перебирает объекты и возвращает одинобъект для каждого цикла без данных.

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'
  }
}];

1 Ответ

0 голосов
/ 14 декабря 2018

Вы можете обращаться к объектам напрямую с заданным id, что совпадает с ключами для объекта с требуемым answer.

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' } }],
    result = data.map(o => ({
        managingRole: o['110'].answer || null,
        advice: o['112'].answer || null
    }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...