如何从哈希表中存储和获取数据-javascript

如何从哈希表中存储和获取数据-javascript,javascript,file-io,hashmap,Javascript,File Io,Hashmap,我在javascript上找不到任何东西,但这可能会很快得到澄清。我在一个网站上工作,在那里我必须通过http请求从服务器检索数据。因为我需要发出几个请求,并且数据是恒定的,所以我要做的是用键和值创建表->将这些值存储到文件->然后能够检索这些值。这样,我必须读取一个文件,以通过30个http请求获取数据 总体思路: Given: spell id = number (Ex. 45) Output: name of spell = string (Ex. fire...) Use this o

我在javascript上找不到任何东西,但这可能会很快得到澄清。我在一个网站上工作,在那里我必须通过http请求从服务器检索数据。因为我需要发出几个请求,并且数据是恒定的,所以我要做的是用键和值创建表->将这些值存储到文件->然后能够检索这些值。这样,我必须读取一个文件,以通过30个http请求获取数据

总体思路:

Given: spell id = number (Ex. 45)
Output: name of spell = string (Ex. fire...)

Use this output to then fetch the url of the image of the spell (containing the spell name)
编辑

代码:

// When I fetch the file i use (json data)
    function getChampionImage(id) {
                    var champUrl = "https://na.api.pvp.net/api/lol/static-data/na/v1.2/champion/" +id +"/?api_key=....."
                    xmlHttp = new XMLHttpRequest();
                    xmlHttp.open( "GET", champUrl, false );
                    xmlHttp.send( null );
                    var jsonText = xmlHttp.responseText;
                    var champData = JSON.parse(jsonText);
                    var champName = champData.key;
                    return "http://ddragon.leagueoflegends.com/cdn/" + versionNum + "/img/champion/"+champName+".png";


                }

你所需要做的就是把响应钉在一个对象上,由输入键控。 这在同步IO中尤其简单,因为您不必担心回调范围

由于函数是JS中的对象,因此您可以将过程本身用作命名空间,这对方法很好,因为您不需要外部变量:

// When I fetch the file i use (json data)
    function getChampionImage(id) {
                    var cached=getChampionImage["_"+id];
                    if(cached) return cached;
                    var champUrl = "https://na.api.pvp.net/api/lol/static-data/na/v1.2/champion/" +id +"/?api_key=....."
                    xmlHttp = new XMLHttpRequest();
                    xmlHttp.open( "GET", champUrl, false );
                    xmlHttp.send( null );
                    var jsonText = xmlHttp.responseText;
                    var champData = JSON.parse(jsonText);
                    var champName = champData.key;
                    return getChampionImage["_"+id]="http://ddragon.leagueoflegends.com/cdn/" + versionNum + "/img/champion/"+champName+".png";

                }
编辑:要在页面访问之间保留url数据,请使用localStorage作为哈希对象:

function getChampionImage(id) {
                var cached=localStorage["_"+id];
                if(cached) return cached;
                var champUrl = "https://na.api.pvp.net/api/lol/static-data/na/v1.2/champion/" +id +"/?api_key=....."
                xmlHttp = new XMLHttpRequest();
                xmlHttp.open( "GET", champUrl, false );
                xmlHttp.send( null );
                var jsonText = xmlHttp.responseText;
                var champData = JSON.parse(jsonText);
                var champName = champData.key;
                return localStorage["_"+id]="http://ddragon.leagueoflegends.com/cdn/" + versionNum + "/img/champion/"+champName+".png";


            }

请注意,使用localStorage,如果进行更改,则必须使缓存自过期。

如果发布文件加载代码,我将向您展示如何集成缓存,这非常简单。我添加了获取json的代码,然后获取url中使用的数据。我不知道这是否就是你所说的,或者javascript对象不是哈希表。但是当我重新加载页面时,缓存会保留吗?我觉得如果我重新加载页面,缓存就会被清除,对吗?编辑-因为如果我在一个客户机上找到champ-我希望它位于访问网站的客户机的缓存中,我没有注意到关于持久性的部分,但这也很简单,我将在editok中向您展示。是的,如果我能把它留在缓存里。我得走了,但我一小时后回来。您可以编辑响应。格雷西丝你说的自我失效是什么意思sry这是一个糟糕的问题-这是否意味着暂时也要编辑缓存…此localStorage变量是否已自动生成为html格式?是的,localStorage随浏览器提供,几乎所有这些。我的观点是,一旦它被缓存在这个系统中,它就再也不会被提取,所以如果你想更新数据,你必须手动删除localStorage键,或者通过添加一个随机查询字符串参数来更改url,这样缓存就会丢失它。如果给定URL后面的数据始终没有更改,则忽略整个警告。