Home > Blockchain >  How to get an objects out from very complex deeply nested array of objects and arrays?
How to get an objects out from very complex deeply nested array of objects and arrays?

Time:07-13

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

CodePudding user response:

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);

  • Related