0
const course = [{
    id: 1,
    name: 'Half Stack application development',
    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: 'Node.js',
    id: 2,
    parts: [
      {
        name: 'Routing',
        exercises: 3,
        id: 1
      },
      {
        name: 'Middlewares',
        exercises: 7,
        id: 2
      }
    ]
  }
]

I have found it is easy to map the names "Half Stack Application Development" and "Node.js" and displaying them by doing:

{course.map(c => <li>{c.name}</li>)}

However, course.parts.map(c => <li>{c.name}</li>) returns undefined.

How would you map it so it looks like

Half Stack Application Development

  • Fundamentals of react
  • Using props to pass data
  • State of a component

Node.js

  • Routing
  • Middleware

Why does course.parts.map(p => p.name) return undefined and how can you access the names in the parts array?

1
  • 1
    course is an array. It doesn't have a parts property. Every element within the array has a parts property. It's not clear how exactly you want the output to be in this case, given that there is more than one part per course. Commented Apr 28, 2022 at 10:40

3 Answers 3

1

Presented below is a working demo using stack-snippets and ReactJS (v 16.8.0) that achieves the desired target.

const Thingy = ({courses, ...props}) => {
  return (
    <div>
      {courses.map(c => (         // iterate over the "courses" array
        <div>
          <h4>{c.name}</h4>
          {c.parts.map(p => (     // now, for each "course", iterate over "parts" array
            <li>{p.name}</li>
          ))}
        </div>
      ))}
    </div>
  );
};

const course = [{
    id: 1,
    name: 'Half Stack application development',
    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: 'Node.js',
    id: 2,
    parts: [
      {
        name: 'Routing',
        exercises: 3,
        id: 1
      },
      {
        name: 'Middlewares',
        exercises: 7,
        id: 2
      }
    ]
  }
]

ReactDOM.render(
  <div>
    DEMO
    <Thingy courses={course} />
  </div>,
  document.getElementById("rd")
);
<div id="rd" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.0/umd/react-dom.production.min.js"></script>

Explanation

Inline comments added to the snippet above.

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

Comments

0

You would need to do a map within a map.

This should work:

   <ul>
    {course.map(c => (
      <>
        <li>{c.name}</li>
        <ul>
          {c.parts.map(p =>
            <li>{p.name}</li>)}
        </ul>
        </>
    ))}
    </ul>
    

Comments

0

course and parts both are arrays, so you cannot get course.parts directly. You can do double map for the renderings like below

{course.map(c => <>
   <p>{c.name}</p>
   <ul>
      {c.parts.map(part => <li>{part.name}</li>)}
   </ul>
</>)}

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.