1

I'm a beginner at node.js and ran into an error. So I have an object that is called roomCards and stores details related to the cards that players have using the player id as a key and their card as a value. Now the issue is, I can't access the player cards by using exactly the same playerId as it comes up as undefined. Here is the object I'm dealing with.

roomCards = {
roomId:5,
deckCards:[ '2C', '2D',  '2H',  '2S',  '3C', '3D',
      '3H', '3S',  '4C',  '4D',  '4H', '4S',
      '5C', '5D',  '5S',  '6C',  '6D', '6H',
      '6S', '7C',  '7D',  '7H',  '7S', '8C',
      '8D', '8H',  '8S',  '9C',  '9D', '9H',
      '9S', '10D', '10H', '10S', 'AC', 'AD',
      'AH', 'AS',  'JC',  'JH',  'JS', 'KD',
      'KH', 'KS',  'QC',  'QD',  'QH', 'QS'],
'W48PoPabFUSN9r-iAAAC': [ '5H', 'JD', 'KC' ] --------- this is the value i'm trying to access using the playerId key 
}

Any help would be greatly appreciated. Have an amazing day ahead :)))

3
  • Please show what you tried that isn't working and provide the specific error details as per minimal reproducible example Commented Aug 22, 2020 at 18:16
  • @charlietfl, Apologies for that, I keep getting an "undefined" value whenever I try to access the value of my playerId : 'W48PoPabFUSN9r-iAAAC' Commented Aug 22, 2020 at 18:31
  • Try to access it with what code? Commented Aug 22, 2020 at 18:35

2 Answers 2

1

Whenever you are using a string literal as a key, the regular dot operator (.) doesn't work. In such cases, you will have you reference such keys using [].

So, in your case, assuming that the player id is stored in a variable name playerId, you can reference to that particular property using:

roomCards[playerId].

var roomCards = {
roomId:5,
deckCards:[ '2C', '2D',  '2H',  '2S',  '3C', '3D',
      '3H', '3S',  '4C',  '4D',  '4H', '4S',
      '5C', '5D',  '5S',  '6C',  '6D', '6H',
      '6S', '7C',  '7D',  '7H',  '7S', '8C',
      '8D', '8H',  '8S',  '9C',  '9D', '9H',
      '9S', '10D', '10H', '10S', 'AC', 'AD',
      'AH', 'AS',  'JC',  'JH',  'JS', 'KD',
      'KH', 'KS',  'QC',  'QD',  'QH', 'QS'],
'W48PoPabFUSN9r-iAAAC': [ '5H', 'JD', 'KC' ]
};

var playerId = 'W48PoPabFUSN9r-iAAAC';

console.log(roomCards[playerId]);

Sign up to request clarification or add additional context in comments.

12 Comments

@chetan7, I have tried doing this, but it still returns undefined instead of the array of cards.
Where is the playerId coming from? It is possible that the playerId you are looking for might not be existing in the array of objects you are maintaining. (In your example, you have only one object). It will be helpful if you can share the code snippet of where and how you are trying to access playerId.
@chetan7 Thanks for your reply. The object I'm trying to access the id from keeps changing the id's since the id is assigned to each new client that is connected to my server. Now before I try accessing the playerID from my object, I do pring out the object, and my "id" is in the object. So it is very weird that I get an undefined when I try to access the value of the id.
You probably want to maintain an array (or store the object in DB for the given session) of such objects. Without the code of how you are trying to access, this will be difficult to answer.
@chetan7 for(let i=0;i<roomDetails.length;i++){ if(parseInt(roomDetails[i].roomId) === parseInt(data.roomId)){ const playerId = roomDetails[i].id; console.log("This is woring",roomCards['"'+playerId+'"']) io.sockets.connected[playerId].emit('playerCards',roomCards['"'+playerId+'"']); } }
|
0

try this

roomCards["W48PoPabFUSN9r-iAAAC"]

Comments

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.