Search code examples
javascriptarraysreactjsreact-routerimmutable.js

Access Object in Array in React Using Immutable.js


I'm trying to access an objects in array from JSON, right now I'm using React and Immutable w/ Typescript and i'm able to access the object using

const MentorContracts = MentorInfo && (MentorInfo.get("contractsAwardedToSdbTotals") as Map<{}, {}>);

however in my JSX, i'm unable to access the data like this

<Typography>FY:{MentorContracts.get("fiscalYear")}</Typography>

How would I access contractsAwardstoSdbTotals.fiscalYear?

here is the JSON data

"mentor": {
    "address": {
      "address": "string",
      "city": "string",
      "state": "string",
      "zip": "string"
    },
    "cageCode": "string",
    "contractsAwardedToSdbTotals": [
      {
        "amount": 0,
        "contractType": "DOD_SUB",
        "fiscalYear": 0,
        "percentage": 0
      }
    ],

Solution

  • Since MentorContracts is a List, you need to map over it and render the desired result

    render() {
       const MentorContracts = MentorInfo && MentorInfo.get("contractsAwardedToSdbTotals");
       return (
           <div>
              {MentorContracts.map((elem) => (
                   <Typography>FY: {elem.get("fiscalYear")}</Typography>
               ))}
           </div>
       )
    }