I'm looking for a JavaScript data structure like ListOrderedMap: http://commons.apache.org/collections/apidocs/org/apache/c开发者_运维知识库ommons/collections/map/ListOrderedMap.html
E.g. It needs to be able add an object at an index, get the index for an object, and able to look up a object by it's id.
All the libraries I could find couldn't add an object at a certain index.
Something like this? Javascript has excellent facilities for arrays and keyed collections, and combinations thereof.
function LAM() {
this.ids = {}
this.indexes = []
}
LAM.prototype.put = function(myObj, id, ix) {
this.ids[id] = myObj
this.indexes[ix] = id
}
LAM.prototype.getByIndex = function(ix) {
return this.ids[this.indexes[ix]]
}
In practice:
? a = new LAM
? a.put("jhgf", "WE", 3)
? a.ids.WE
jhgf
? a.getByIndex(3)
jhgf
Since Javascript doesn't have such the data structure, another solution is to use GWT and use the Java source code of ListOrderedMap.java.
精彩评论