0

When I print my allusers list I get this :

allusers {
           2BrbqqlkVf1NSocm58qr95iL5h2: {…}, 
           7VQMBXdTPIZZZ8ztOLx9JEeRty1: {…}, 
           HNXrFSF3YoT07qapsz3xE9ODGp2: {…}, 
           NwiWN3xDq3awRghWscVBEmxaclB2: {…}, 
           P7P4Vc5hy1ZrYY9YDB5AfNIpQf2: {…},
 …}

each one of the ids look like this :

2BrbbqqlkVf1NSocm58q95iL5h2: {Username: "ee", Email: "ee@gmail.com"}

I have the id stored in candidateid , how do I get the user name? I tried this:

<Button onClick={() => setchosen(candidateid)}>
  <h1>{allusers.candidateid.Username}</h1> 
</Button>

I also tried using .map and it doesn't work

Frank van Puffelen
  • 499,950
  • 69
  • 739
  • 734
Xayyna
  • 41
  • 7
  • 3
    `allusers[candidateid].Username` – ChrisG Jul 16 '20 at 09:57
  • Does this answer your question? [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – ChrisG Jul 16 '20 at 09:58

2 Answers2

1

You can't use candidateId as a property-access because javascript will try to find a property on allUsers with the literal name candidateId.

What you want to do is:

allusers[candidateId].Username

That uses the value saved in candidateId.

Christian
  • 22,099
  • 9
  • 78
  • 104
1

You can easily do:

allusers[candidateId].Username
tsiwant
  • 11
  • 3