I need a data structure to store several JavaScript objects, and to be able to access them with a string id (get/set/delete operations).
Here is an example of the items I need to store :
var Player = function(name) {
this.name = name;
this.x = 0;
this.y = 0;
this.toString = function() {
return 'player : ' + this.name + ' at ' + this.x + ', ' + this.y;
};
}
I would like to store players in a data structure and to be able to get/set/delete them by their name, like players.get('Bob')
to get the player with Bob as name.
At first, I thought I could use a map with the name as key (I'm using Dict from collectionsjs). But then I can't access the name from the methods of the item (toString
in my example).
I could use a regular Array, keep the name attribute and implement my own get/set/delete methods, however I would rather use a reliable data structure but I can't find it.
Thanks in advance :]