An array containing objects with attributes that have nested arrays containing more objects with attributes in it.
const courses = [
{
name1: ‘Half Stack application developm
id: 1,
parts: [
{
name: ‘Fundamentals of React’,
exercises: 10,
id: 1
},
{
name: ‘Using props to pass data’,
exercises: 7,
id: 2
},
{
name: ‘State of a component’,
exercises: 14,
id: 3
},
{
name: ‘Redux’,
exercises: 11,
id: 4
}
]
},
{
name1: ‘Node.js’,
id: 2,
parts: [
{
name: ‘Routing’,
exercises: 3,
id: 1
},
{
name: ‘Middlewares’,
exercises: 7,
id: 2
}
]
},
]
How to destructure all of the objects from the nested arrays?
So far I can only get the attributes of the first object from the nested arrays. But I would need all of the others objects. It works well for the parent array even when updating the array, but not for the children arrays containing the objects.
for (const {
name1: n,
parts: [{ name: n2, exercises: ex }],
} of courses) {
console.log(`${n}:
${n2} ${ex}`);
}
ACTUAL RESULT:
Half Stack application development:
Fundamentals of React 10
Node.js:
Routing 3
EXPECTED RESULT:
Half Stack application development:
Fundamentals of React 10
Using props to pass data 7
State of a component 14
Redux 11
Node.js:
Routing 3
Middlewares 7