1

I have an Object like;

  players: {
    '1': {id:1, name:'', ....},
    '2': {id:2, name:'', ....},
     ... 
   }

I want to desctruct an object by its key as currentPlayer. (playernumber is passed as props).

const currentPlayer = Object.keys(players).filter(
      obj => obj.id === playerNumber
    );

this did not work, also I do not want to use id attribute.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
Amir-Mousavi
  • 4,273
  • 12
  • 70
  • 123
  • Possible duplicate of [Difference between using bracket (\`\[\]\`) and dot (\`.\`) notation](https://stackoverflow.com/questions/17189642/difference-between-using-bracket-and-dot-notation) – Code-Apprentice Aug 30 '18 at 04:21
  • @Code-Apprentice Yes, but I did not even know I can use [] for objects, though it is only for Array. new in JS :) – Amir-Mousavi Aug 30 '18 at 04:43

3 Answers3

2

The easiest way to get a specific player is with bracket notation:

const currentPlayer = players[playerNumber];

This assumes that playerNumber is a valid key in the players object.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
1

Could you not just use Object.values() to achieve this?

The Object.values() will return the values of your players object ({id:1, name:'', ....}, etc) as an array. You can then use the .filter() method to select the player value by playerNumber.

So for instance, something like this:

const currentPlayer = Object.values(players).filter(
  obj => obj.id === playerNumber
);

You will find that this works in most browsers

Alternativly, if you have your players object organised so that the keys are player id's, you can access a player in this way:

const currentPlayer = players[playerNumber];
Dacre Denny
  • 29,664
  • 5
  • 45
  • 65
0

Object.keys() will give you an array of keys rather than the object stored under those keys (i.e. Object.keys(players) returns ['1', '2', ...]).

If you want to get the objects, you can map the result array like so:

// Use map to turn the array of keys into the array of objects
const currentPlayer = Object.keys(players).map(id => players[id])
  // be careful: filter returns an array, but you probably just want the one object
  .filter(obj => obj.id === playerNumber)[0]

Depending on what you're targeting, you may also have access to Object.values() which does the same thing (i.e. you'd replace the first line with just Object.values(players)), just be aware that browser support is a little more limited.

zkcro
  • 4,344
  • 1
  • 24
  • 22
  • Thanks yes I want only the object, so how can I avoid using the id attribute. for instance, if the keys are still numbers, but the object does not contain id number – Amir-Mousavi Aug 30 '18 at 04:16
  • Not clear on what you mean by avoiding using the id attribute. – zkcro Aug 30 '18 at 04:38
  • Never mind, clear now from the accepted answer. Had assumed there was a specific reason you were avoiding doing it that way, rather than just not being clear on how to do it :) – zkcro Aug 30 '18 at 04:38
  • Thanks, yes I am new in javascript, thinking [] is only used for arrays. – Amir-Mousavi Aug 30 '18 at 04:40