Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
Map object values from an array within an array
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?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report

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>
</>)}
about 4 years ago · Juan Pablo Isaza Report

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>
    
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!