Mr. Melo raise a good question for JavaScript Object Access Pattern.
I leave the prior 'introductory' answer for others to interpret your code.
My answer is to override a print method, and another way, to convert the data into a human-readible data-translation.
when print it or send it by json you get non sensible output:
{,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,minion,,,,,,,,,,,,,,minion}
Here is where I can give a suggestion.
What you are seeing above is the Object-Literal converted to string. Perhaps nested in a JSON.stringify() method or another perhaps toString() use. You can improve this with two potential solutions depending on your context.
A ToString Method Override. - One solution is to dig into the function (step-debug) 'down-into' the method that is converting your object to String, and copy, rename, and change it to yourOwnStringifyMethod(). This is an essentially a toString() method override. Instead of printing
","+","+","+data+",",
override it to print something else. Call it minion.minionToString() or something.
Indices could easily be inserted in the iterator to produce:
{0,1,2,data,...}
Or Print the id of a minion by printing the data-members like +minion.name+
. Search for toString override, this would be perhaps ideal, if you are transferring the JSON asynchronously, or real-time web-sockets, and just want to translate the data.
B. Templates. If the data is to be viewed on screen, instead of transferred, data-bind it from json to a template. Data-binding is great. The syntax is something like var arg = " <html>...<div><%data-field*></div></html>"
Then you send that into a templ(arg) function, for example, and it spits out a var x = templ(htmltemplate, data-stream)
. Var x is now your template-data-loaded-html to be inserted into the dom.
There are a number of ways to do this.
C. This is an interesting incarnation of code where you hint:
i've been keeping in an array where the "location" is the id
That reminds me somehow of CouchDB. CouchDB and Mongo are JavaScript Databases, that use JSON objects persisted over time by a long unique ID. Maybe this will help you evolve an accessor and display best-practice.
D. The lookup function loop looks potentially costly for very-large data-sets. There are certain indexing and search algorithms that can cut down on processing iterations if you are running a high number of minions, and wish to optimize lookup. Paul Irish wrote a book, or paper, on 10 things that I learned by reading jQuery source code. I imagine there is some advanced fruit in there about efficient dom node traversal, which could, in the least, be enlightening to your armament of minions.
Hope that helps.
All the best!
Nash