Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

97
Vistas
Mapping through Object that is passed to child as props

I am trying to map through an object that is passed as prop and want to display different HTML elements for different object values

Object;

         const allDesc =  {description: "Title Description", description1: "Intro 
    Description", description3:"Sub title", description3: "Sub Description"}

Code:

      <div>
        {Object.keys(allDesc).map((desc, index) => {
          if (allDes[desc] !== "") {
            return (
              <>
                <h1>allDesc.description</h1>
                <p>allDesc.description1</p>
                <h3>allDesc.description2</h3>
                <p>allDesc.description3</p>
              </>
            );
          }
        })}
      </div>

This approach displays nothing, what would be the correct approach for mapping through an object and displaying different HTML elements for different object values. Thanks!

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Please try this.

<div>
        {Object.keys(allDesc).map((desc, index) => {
          if (allDes[desc] !== "") {
            return (
              <>
                {
                 desc === 'description' ? <h1>allDes[desc]</h1> : 
                 <p>allDes[desc]</p>
                } 
              </>
            );
          }
        })}
      </div>

this allDes[desc] is have the value of all field you loop from the Object keys.

about 4 years ago · Juan Pablo Isaza Denunciar

0

map over the Object.entries. If the key matches "description" return the value as an <h1>, otherwise return the value as a <p>.

function Example({ allDesc }) {
  return (
    <div>
      {Object.entries(allDesc).map(([key, value]) => {
        if (key === 'description') return <h1>{value}</h1>;
        if (key === 'description3') return <h3>{value}</h3>;
        return <p>{value}</p>;
      })}
    </div>
  );
};

const allDesc = {description: 'Title Description', description1: 'Intro Description', description3: 'Another Description', description4: 'More text' };

ReactDOM.render(
  <Example allDesc={allDesc} />,
  document.getElementById('react')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<div id="react"></div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda