最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Something like a Map<String, Object> I can use to store data in jquery? - Stack Overflow

programmeradmin1浏览0评论

I have some JSON objects I'd like to store in a map for the lifetime of my app. For example, my app shows a listing of Farms. When a user clicks one of the Farm links, I download a Farm representation as JSON:

Farm 1
Farm 2
...
Farm N

every time the user clicks one of those links, I download the entire Farm object. Instead, I'd like to somehow make a global map of Farms, keyed by their ID. Then when the user clicks one of the above links, I can see if it's already in my map cache and just skip going to the server.

Is there some general map type like this that I could use in jquery?

Thanks

I have some JSON objects I'd like to store in a map for the lifetime of my app. For example, my app shows a listing of Farms. When a user clicks one of the Farm links, I download a Farm representation as JSON:

Farm 1
Farm 2
...
Farm N

every time the user clicks one of those links, I download the entire Farm object. Instead, I'd like to somehow make a global map of Farms, keyed by their ID. Then when the user clicks one of the above links, I can see if it's already in my map cache and just skip going to the server.

Is there some general map type like this that I could use in jquery?

Thanks

Share Improve this question asked Jul 16, 2010 at 18:37 user246114user246114 51.6k43 gold badges118 silver badges152 bronze badges 1
  • See here: stackoverflow.com/questions/368280/… – Java Drinker Commented Jul 16, 2010 at 19:51
Add a comment  | 

3 Answers 3

Reset to default 14

What about a JavaScript object?

var map = {};

map["ID1"] = Farm1;
map["ID2"] = Farm2;
...

Basically you only have two data structure in JavaScript: Arrays and objects.

And fortunately objects are so powerful, that you can use them as maps / dictionaries / hash table / associative arrays / however you want to call it.

You can easily test if an ID is already contained by:

if(map["ID3"]) // which will return undefined and hence evaluate to false

The object type is the closest you'll get to a map/dictionary.

var map={};
map.farm1id=new Farm(); //etc
farmMap = {};
farmMap['Farm1'] = Farm1;
...
发布评论

评论列表(0)

  1. 暂无评论