nathan
nathan

Reputation: 379

How to access an object inside an array of array

I am trying to access to an object in side an array of array. Here is how my console.log showing:

0:
Memberships: Array(1)
0:
teamId: "5413e75f-ff12-4b7a-a3fe-f892cd006366"
__proto__: Object
length: 1
__proto__: Array(0)
Ratings: []
Skills: []
fullname: "Nhan Nguyen"
institution: ""
linkedin: ""
location: ""
major: ""

So now I want to get access to teamId which is inside an array of array, here is my following code:

{invitees?.map((user, index) => (
  <Grid item className="invitees-card" key={index}>
    {user.Memberships[0].teamId !== teamId && (
    <InviteCard tab={tab} user={user} />
    )}
  </Grid>
))}

it gave me .teamId is undefined which is I think my syntax is wrong but is there any way I can access to teamId without doing another mapping?

Upvotes: 0

Views: 64

Answers (1)

T J
T J

Reputation: 43166

Some of the objects don't have a membership property so you should use a null safe operator: user?.Membership[0]?.teamId

Or do user.Membership && user.Membership[0].teamId to guard against undefined

Upvotes: 1

Related Questions