1

I have an array with an object inside that houses more objects that I would like to map to a simpler structure.

I have tried with the following and it seems to loop through the objects and return a single object for each loop with no data.

Object.values(data).forEach((a) => {
  console.log({
    managingRole: a.id === 110 ? a.answer : null,
    advice: a.id === 112 ? a.answer : null,
  });
});

The end goal is to return an array of object to map through like:

const desired = [{
  managingRole: 'spending',
  advice: 'everyone'
},
{
  managingRole: 'saving',
  advice: 'no one'
}];

The data I an working with is below:

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'
  }
}];
5
  • You mentioned the word "map" several times. Did you try Array#map()? A web search for "javascript map array" would have had this right at the top along with numerous questions on this site also Commented Dec 14, 2018 at 19:27
  • 1
    There's no need to use Object.values(data) to loop over an array. Just use data.forEach(). Commented Dec 14, 2018 at 19:28
  • You also need Array#filter() to remove the elements that you don't need in the result. Commented Dec 14, 2018 at 19:31
  • I don't really understand the relationship between the input and output. It looks like a single object in the output has properties from different objects in the input. I think they're related by having the same id property. Commented Dec 14, 2018 at 19:33
  • You need to use Object.values(a)[0] to get the property in the object. Why do you have an array of objects where each object has a different key? Commented Dec 14, 2018 at 19:35

1 Answer 1

1

You could address the objects directly with the given id which is the same as for the keys for the object with the wanted 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; }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.