I am trying to describe a Pokemon move "database" with javascript to practice data description.
I have a bunch of moves, identified by an id(number)., name(string), and type(string).
I want to create a javascript array of moves such as this(use array index as id for O(1) lookup by id):
const moves = [
{name: 'Astonish', type: 'Ghost'},
{name: 'Constrict', type: 'Normal'},
{name: 'Acid', type: 'Poison'},
{name: 'Ingrain', type: 'Grass'}
];
However, I want to ensure that the index of these entries is strictly positioned(to avoid pasting an entry into the start of the array, invalidating the indexes, as well as making it more explicit what the entries are so you don't need to do a linear search to find the item each time you look at the list.
What I want to do is this, but it is not supported by JavaScript syntax:
const moves = [
0: {name: 'Astonish', type: 'Ghost'},
1: {name: 'Constrict', type: 'Normal'},
2: {name: 'Acid', type: 'Poison'},
3: {name: 'Ingrain', type: 'Grass'}
];
Doing this by explicitly inserting each item seems expensive and visually annoying:
const moves = [];
/* anonymous scope: setup moves */
{
moves[0] = {name: 'Astonish', type: 'Ghost'};
moves[1] = {name: 'Constrict', type: 'Normal'},
moves[2] = {name: 'Acid', type: 'Poison'},
moves[3] = {name: 'Ingrain', type: 'Grass'}
}
I could use an object, but objects are more expensive than arrays for this kind of behavior. I could also create an object then map it into an array like this:
const moves = (() => {
let res = [];
let tmp = {
0: {name: 'Astonish', type: 'Ghost'},
1: {name: 'Constrict', type: 'Normal'},
2: {name: 'Acid', type: 'Poison'},
3: {name: 'Ingrain', type: 'Grass'}
};
Object.keys(tmp).map ((key) => {
res[key] = tmp[key];
});
return res;
})();
but it seems a tad silly.
The reason why I want to have array index as id is because it would both be fast to access, and make it easy to make another object to map back the move names to their id(for O(1) move by name lookup).
Is there a way to describe this kind of array in a more coherent way, or are these the only options I have without inventing my own javascript convention, preprocessor?
Thanks ahead of time.
Related: