Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

70
Visualizações
How to display array data that varies in length

I got 2 objects inside the array, and 1st object is longer than the 2nd object. How can i render all of the properties of the 1st object without getting undefined, i get undefined because there are only 2 properties existing in the second object of the array .Also how can i calculate total sum of exercises?

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

  // calculate total of exercises
  const totalExercises = course.reduce((total, course) => total + course.exercises, 0);

  return (
    <div className="App">
      <header className="App-header">
        <h1>Seoul</h1>
        <Course course={course} totalExercises={totalExercises} />
      </header>
    </div>
  )
}
function Course({ course, totalExercises }) {
  return (
    <>
      <ul>
        {course.map((course) => (
          <li key={course.id}>
          <p>{course.name} {course.exercises}</p>
          <p>{course.parts[0].name}</p>
          <p>Total exercises: {course.parts[0].exercises},</p>
          <p>{course.parts[1].name}</p>
          <p>Total exercises: {course.parts[1].exercises}</p>
          // Undefined one below
          UNDEFINED <p>{course[0].parts[2].name}</p>
          </li>
        ))}
      </ul>
    </>
  );
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You could use map the parts array to the elements:

function Course({ course, totalExercises }) {
  return (
    <>
      <ul>
        {course.map((course) => (
          <li key={course.id}>
          <p>{course.name} {course.exercises}</p>
          {
              course.parts.map((part, id)=>(
                  <React.Fragment key={id}
                      <p>{part.name}</p>
                      <p>Total Excercises: {part.exercises}</p>
                  </React.Fragment>
              ))
          }
          </li>
        ))}
      </ul>
    </>
  );
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You try to render an array manually... it's a bad idea imagine that your array is dynamic how you can anticipate the number of elements in the array?

Done as follows.

function Course({ course, totalExercises }) {
  return (
    <>
      <ul>
        {course.map((course) => (
          <li key={course.id}>
          <p>{course.name} {course.exercises}</p>
          {course.parts?.map((part, index) => (
          <div key={index}>
            <p>{part.name}</p>
            <p>Total exercises: {part.exercises},</p>
          </div>
          ))}
          </li>
        ))}
      </ul>
    </>
  );
}

I hope my English doesn't tire you, I'm French-speaking

about 4 years ago · Juan Pablo Isaza Relatório

0

function Course({ course, totalExercises }) {
    return (
      <>
        <ul>
          {course.map((course) => (
            <li key={course.id}>
            <p>{course.name} {course.exercises}</p>
                {course.parts.map((part,i) => {
                    return(
                        <div key={i}>
                            <p>{part.name}</p>
                            <p>Total exercises: {part.exercises},</p>
                        </div>
                    )
                })}
            </li>
          ))}
        </ul>
      </>
    );
  }

Same way that you are mapping course.map(... you can then map the parts for each course, code above works without an error for me.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda