Reputation: 11151
I am working on an app that uses ES 6 JavaScript. This app has a list of games. Each game has an ID. The objects are defined like this:
let game1 = { id:'XVF/328', name:'Checkers', publisher:'ABC games' };
let game2 = { id:'FTY/294', name:'Chess', publisher:'Infinity Fun' };
let game3 = { id:'QPG/72Z', name:'Backgammon', publisher:'Max Fun' };
I have an array that looks like this:
let games = [];
I'm trying to understand how to populate this array for fast access. I no that I'll get an ID, like 'FTY/294'. I'm trying to understand how to setup the games array so that I can do something like:
let selectedGame = games['FTY/294'].
How can you do this in JavaScript?
Thanks!
Upvotes: 0
Views: 139
Reputation: 67525
You could use ids as keys in new object :
games = {};
games[game1.id] = game1;
games[game2.id] = game2;
games[game3.id] = game3;
Hope this helps.
game1 = { id:'XVF/328', name:'Checkers', publisher:'ABC games' };
game2 = { id:'FTY/294', name:'Chess', publisher:'Infinity Fun' };
game3 = { id:'QPG/72Z', name:'Backgammon', publisher:'Max Fun' };
games = {};
games[game1.id] = game1;
games[game2.id] = game2;
games[game3.id] = game3;
console.log(games);
Upvotes: 0
Reputation: 3663
In javascript, objects behave much like HashTable
or Dictionary
in Java/C#. You use store values under a key, and use that key to later retrieve the value.
If you use an object rather than an array then you get key->value access.
let games = {};
games['XVF/328'] = { id:'XVF/328', name:'Checkers', publisher:'ABC games' };
games['FTY/294'] = { id:'FTY/294', name:'Chess', publisher:'Infinity Fun' };
If you need to iterate over all the games you can use Object.keys(games)
to retrieve a list of all keys, and then use that to iterate your object.
Object.keys(games).forEach(x => {
var game = games[x];
// do something with game
});
In ES6 you also have the option of using Map which gives you the benefits of an associative array (as used above) but with the addition of iterable support.
var games = new Map();
games.set('XVF/328', { id:'XVF/328', name:'Checkers', publisher:'ABC games' });
// ....etc
for (let [id, game] of games) {
// do something with the game
}
Upvotes: 5
Reputation: 3816
If you are using ES6, then you should definitely use Maps
const games = new Map(); // or let
games.set('XVF/328', { id:'XVF/328', name:'Checkers', publisher:'ABC games' })
games.set('FTY/294', { id:'FTY/294', name:'Chess', publisher:'Infinity Fun' };
games.set('QPG/72Z', { id:'QPG/72Z', name:'Backgammon', publisher:'Max Fun' };
let game = games.get('FTY/294');
Upvotes: 0
Reputation: 76577
You might want to consider using an object as an associative array for this, which functions similar to a dictionary (i.e. you pass in a key and you receive a value in return) :
// Define your object
let games = {};
// Add each game here
games['XVF/328'] = { name:'Checkers', publisher:'ABC games' };
games['FTY/294'] = { name:'Chess', publisher:'Infinity Fun' };
games['QPG/72Z'] = { name:'Backgammon', publisher:'Max Fun' };
And then simply retrieve the game you are looking for via :
let selectedGame = games[id]; // where id is something like "XVF/328"
Upvotes: 2