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',
},
]
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.