Get a value from an object using a variable as a key

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Get a value from an object using a variable as a key



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.


id





Possible duplicate of Difference between using bracket (``) and dot (`.`) notation
– Code-Apprentice
7 mins ago




3 Answers
3



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.


playerNumber


players





Perfect, Thanks a lot
– Amir-Mousavi
8 mins ago



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


Object.values()



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.


Object.values()


players


id:1, name:'', ....


.filter()


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:


players


const currentPlayer = players[playerNumber];





Thanks, the Documentation was saying values() returns array[id,1,name,...] but it returning Object now.
– Amir-Mousavi
10 mins ago





how can I not use id attribute and just the key which is number. if my sub objects did not have id attr
– Amir-Mousavi
9 mins ago





Hi @Amir-Mousavi , please see updated answer
– Dacre Denny
8 mins ago





Thanks, it is what I was looking for :)
– Amir-Mousavi
6 mins ago



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', ...]).


Object.keys()


Object.keys(players)


['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.


Object.values()


Object.values(players)





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
12 mins ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard