-1

My data structure is very complex, can't figure out how to map through the data in a way so I can get what I want. There's a very big array with a lot of objects in it and each object has again arrays and objects nested in each other. The data looks like this:

const arr1 = [
  {
    id: 1,
    subject: 'Subject',
    chapters: [
      {
        name: 'chapter1',
        sections: [
          {
            name: 'section1',
            questions: [
              {
                title: 'Title',
                subtitle: 'SubTitle',
              },
              {
                title: 'Title',
                subtitle: 'SubTitle',
              },
            ],
          },
          {
            name: 'section2',
            questions: [
              {
                title: 'Title',
                subtitle: 'SubTitle',
              },
              {
                title: 'Title',
                subtitle: 'SubTitle',
              },
            ],
          },
        ],
      },
      {
        name: 'chapter2',
        sections: [
          {
            name: 'section1',
            questions: [
              {
                title: 'Title',
                subtitle: 'SubTitle',
              },
              {
                title: 'Title',
                subtitle: 'SubTitle',
              },
            ],
          },
          {
            name: 'section2',
            questions: [
              {
                title: 'Title',
                subtitle: 'SubTitle',
              },
              {
                title: 'Title',
                subtitle: 'SubTitle',
              },
            ],
          },
        ],
      },
    ],
  },
  {
    id: 2,
    subject: 'Subject',
    chapters: [
      {
        name: 'chapter1',
        sections: [
          {
            name: 'section1',
            questions: [
              {
                title: 'Title',
                subtitle: 'SubTitle',
              },
              {
                title: 'Title',
                subtitle: 'SubTitle',
              },
            ],
          },
          {
            name: 'section2',
            questions: [
              {
                title: 'Title',
                subtitle: 'SubTitle',
              },
              {
                title: 'Title',
                subtitle: 'SubTitle',
              },
            ],
          },
        ],
      },
      {
        name: 'chapter2',
        sections: [
          {
            name: 'section1',
            questions: [
              {
                title: 'Title',
                subtitle: 'SubTitle',
              },
              {
                title: 'Title',
                subtitle: 'SubTitle',
              },
            ],
          },
          {
            name: 'section2',
            questions: [
              {
                title: 'Title',
                subtitle: 'SubTitle',
              },
              {
                title: 'Title',
                subtitle: 'SubTitle',
              },
            ],
          },
        ],
      },
    ],
  },
];

How can I get an array from arr1 with only the objects located in 'questions' array, so it will look like this:

const arr2 = [
  {
    title: 'Title',
    subtitle: 'SubTitle',
  },
  {
    title: 'Title',
    subtitle: 'SubTitle',
  },
  {
    title: 'Title',
    subtitle: 'SubTitle',
  },
  {
    title: 'Title',
    subtitle: 'SubTitle',
  },
  {
    title: 'Title',
    subtitle: 'SubTitle',
  },
  {
    title: 'Title',
    subtitle: 'SubTitle',
  },
  {
    title: 'Title',
    subtitle: 'SubTitle',
  },
  {
    title: 'Title',
    subtitle: 'SubTitle',
  },
]
3
  • I think you will want to look into using something like lodash to help you: stackoverflow.com/questions/31054021/… Commented Jul 12, 2022 at 20:47
  • 1
    3 flatMaps for chapters sections and questions? . btw shouldnt it be 16 results in final? Commented Jul 12, 2022 at 20:52
  • @cmgchess Yes should be 16, thanks! Commented Jul 12, 2022 at 21:01

1 Answer 1

1

Use flatMap

const arr1 = [{id: 1,subject: 'Subject',chapters: [{name: 'chapter1',sections: [{name: 'section1',questions: [{title: 'Title',subtitle: 'SubTitle',},{title: 'Title',subtitle: 'SubTitle',},],},{name: 'section2',questions: [{title: 'Title',subtitle: 'SubTitle',},{title: 'Title',subtitle: 'SubTitle',},],},],},{name: 'chapter2',sections: [{name: 'section1',questions: [{title: 'Title',subtitle: 'SubTitle',},{title: 'Title',subtitle: 'SubTitle',},],},{name: 'section2',questions: [{title: 'Title',subtitle: 'SubTitle',},{title: 'Title',subtitle: 'SubTitle',},],},],},],},{id: 2,subject: 'Subject',chapters: [{name: 'chapter1',sections: [{name: 'section1',questions: [{title: 'Title',subtitle: 'SubTitle',},{title: 'Title',subtitle: 'SubTitle',},],},{name: 'section2',questions: [{title: 'Title',subtitle: 'SubTitle',},{title: 'Title',subtitle: 'SubTitle',},],},],},{name: 'chapter2',sections: [{name: 'section1',questions: [{title: 'Title',subtitle: 'SubTitle',},{title: 'Title',subtitle: 'SubTitle',},],},{name: 'section2',questions: [{title: 'Title',subtitle: 'SubTitle',},{title: 'Title',subtitle: 'SubTitle',},],},],},],},];

const result = arr1.flatMap(({chapters}) => 
    chapters.flatMap(({sections}) => 
        sections.flatMap(({questions}) => questions)
    )
);

console.log(result);

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.