开发者

ListOrderedMap/ArrayMap like data structure in Javascript

开发者 https://www.devze.com 2023-03-23 15:54 出处:网络
I\'m looking for a JavaScript data structure like ListOrderedMap: http://commons.apache.org/collections/apidocs/org/apache/c开发者_运维知识库ommons/collections/map/ListOrderedMap.html

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号