简体   繁体   中英

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
      }
    ],

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>
   )
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM