0

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 ?

1
  • Your error says that contract can be undefined. Have you tried to add an undefined check to it? contract?.reason.map of if(contract !== undefined)contract.reason.map... Commented Sep 22, 2021 at 19:05

2 Answers 2

1

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>
          ),
        },
      };
    });
  }
Sign up to request clarification or add additional context in comments.

2 Comments

After and question mark for contract and reason like contract?.reason?.map... I got error Property 'map' does not exist on type 'string'.ts(2339)
Like the error says, it means that reason is of type string. Can you share what the state looks like?
1

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

contract?.reason?.map

3 Comments

After and question mark for contract and reason like contract?.reason?.map... I got error Property 'map' does not exist on type 'string'.ts(2339)
It means that reason is actually a string.
Check the type of reason property

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.