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

205
Views
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 answers
Answer question

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 Report

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 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!