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

207
Vistas
How to loop through an array in react typescript?

How can I loop through an array to display the value?

Here is my following code:

  ineligiblePointsTableRows() {
    return this.state[PointsTableType.INELIGIBLE].contracts.map(contract => {
      return {
          applied: (<input
              value={0}
              disabled
              className="applied-pts-input"
          />),
          reason: (
            <div className="ineligible-reason-container">
              <i className="fa fa-exclamation-triangle"></i>
              <p aria-label={contract.reason}>{contract.reason.map((reason: any, i: any) => (
                <text>Hello world</text>
              ))}</p>
            </div>
          ),
        },
      };
    });
  }

I got error at line contract.reason.map

Object is possibly 'undefined'.ts(2532) (parameter) contract: ContractUsage No quick fixes available

when I console.log(contract.reaons) this is the value:

Array(2)
0: "Cannot borrow usage this far in the future."
1: "Contract has a past due balance."

So my goal is i am trying to loop through contract.reason but I am having error like above. How can I fix this ?

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

0

It's a typescript error telling you that the object might be "undefined", or in other words, might be empty of any value.

Few possible ways to solve this:

1.

ineligiblePointsTableRows() {
    return this.state[PointsTableType.INELIGIBLE].contracts.map(contract => {
      return {
          applied: (<input
              value={0}
              disabled
              className="applied-pts-input"
          />),
          reason: (
            <div className="ineligible-reason-container">
              <i className="fa fa-exclamation-triangle"></i>
              <p aria-label={contract?.reason}>{contract?.reason?.map((reason: any, i: any) => (
                <text>Hello world</text>
              ))}</p>
            </div>
          ),
        },
      };
    });
  }
ineligiblePointsTableRows() {
    return this.state[PointsTableType.INELIGIBLE].contracts.map(contract => {
      return {
          applied: (<input
              value={0}
              disabled
              className="applied-pts-input"
          />),
          reason: (
            <div className="ineligible-reason-container">
              <i className="fa fa-exclamation-triangle"></i>
              {contract !== undefined && <p aria-label={contract.reason}>{contract.reason.map((reason: any, i: any) => (
                <text>Hello world</text>
              ))}</p>}
            </div>
          ),
        },
      };
    });
  }
about 4 years ago · Juan Pablo Isaza Denunciar

0

It means that your contract or contract.reason is possibly undefined. To avoid the error use optional chaining like this:

contract?.reason?.map

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