1

I'm currently writing a function to pre-load all images used by a small game to draw on an array. Currently I store the paths to the sources in two different array to solve this, but would it be possible to have an array that can use both a number i or a name n when getting a value from it? This would help it be easier to use the value to assign as a search on my pictures later on, and using gameimage[153] as a source value doesn't look very tidy and I'd rather use gameimage["snakehead"]

current code example:

//add images to the gameimages array to be loaded before gamestart
//add the snake images
var gameimages = [];
gameimages.push("snake30px.png", "snake30pxdown.png", "snake30pxup.png","snake30pxauch.png");

var gameimagesnumber = gameimages.length;

//start the startGame() when all images in the gameimages array is loaded, to avoid albino snake and lack of stuff to crash into
//NOTE: This is kinda "hackish" as the images is just loaded to make sure it is cached in browser...
//they're not actually used, but seem to have the same effect :x
for(var i = 0; i < gameimagesnumber; i++){
    console.log("Loading " + gameimages[i]);
    var image = new Image();
    image.onload = function(){
        //add the image in gameimagesnames for easier use in the code when needed
        gameimagesnames[this.src.substring(this.src.lastIndexOf("/") + 1,this.src.length - 4)] = this.src;
        checkforgamestart();
    };
    image.src = "images/" + gameimages[i];
}

//checking for gamestart
function checkforgamestart(){
    if(gameimagesnumber > 1) gameimagesnumber--;
    else startGame();
}
Selbyggen
  • 13
  • 3
  • 1
    Have you looked into creating some sort of [dictionary/hash table](http://stackoverflow.com/questions/1208222/how-do-i-implement-a-dictionary-or-hashtable-in-javascript) or 2D arrays? – freddiev4 Feb 20 '16 at 01:10
  • Or you can have an array of objects with each object having a key and a value, something like: `[{key: 0, val: 'bla'}, {key: 1, val: 'blabla' }]` – nem035 Feb 20 '16 at 01:12
  • In javascript, object keys can be almost anything. So, you can get away with: `var myobj = {'snakehead': somevalue};` To access, use array notation: `myobj['snakehad']`. Basically, each array item becomes an object property (e.g. `{'snakehead': val1, 'snaketail': val2}`). – jbarreiros Feb 20 '16 at 01:17

2 Answers2

3

Absolutely!

In JS, you can make an array of any data type. You also have access to objects. So let's combine those.

var obj = {
    name: 'a test name',
    value: 1
}

var array = [obj];

array[0].name;  // == 'a test name'
array[0].value; // == 1

var anotherObj = {
    name: 'another name',
    value: 7
}

array.push(anotherObj);


array[1].name;  // == 'another name'
array[1].value; // == 7

Reading your question in more detail, I see you're also looking to have a get method that can pull from either value. That's a bit trickier.

The other answer provided will do this, but stores the data in two separate locations in the object (not an array), and also loses the array prototypes.

To better solve this within the Array class type, let's just take advantage of Array.filter!

array.filter(function(item) { return item.name === 'another name' })

This will provide you with a subArray of elements that meet whatever criteria you provide within the given callback function. In this case, using my array above, it would pass back an array with one element in it; anotherObj.

Jake Haller-Roby
  • 6,335
  • 1
  • 18
  • 31
  • yep, javascript was a bit rusty for me.. but of course! – michel.iamit Feb 20 '16 at 01:14
  • Just when started to think I knew how objectoriented programming could benefit me, you do this! Looks great, will try to implement. – Selbyggen Feb 20 '16 at 01:19
  • Provided a little extra info after reading into your use case more. Hope it helps. – Jake Haller-Roby Feb 20 '16 at 01:22
  • 1
    Just figured that myself as well, and started struggling how to get the info back out again. Since there is a filter for an array, would it be possible to use this directly to filter from the value? Removing the need to implement an object (in this case, since all I need is a 2nd value, which will always be in the value anyway). Guess I should take a look at the array.filter function – Selbyggen Feb 20 '16 at 01:27
0

If you want to access by both, use object

var arr = {}
arr[1] = arr['myKey'] = 'myValue'

Then you can access them both by number and by key.

Tuan Anh Tran
  • 6,807
  • 6
  • 37
  • 54