localstorage:获取特定的localstorage值,其中包含许多项 [英] localstorage: Get specific localstorage value of which contains many items
问题描述
在本地存储中,我有键"结果"和该值:
In localstorage I have key 'results' with this values:
[{"id":"item-1","href":"google.com","icon":"google.com"},
{"id":"item-2","href":"youtube.com","icon":"youtube.com"},
{"id":"item-3","href":"google.com","icon":"google.com"},
{"id":"item-4","href":"google.com","icon":"google.com"},
{"id":"item-5","href":"youtube.com","icon":"youtube.com"},
{"id":"item-6","href":"asos.com","icon":"asos.com"},
{"id":"item-7","href":"google.com","icon":"google.com"},
{"id":"item-8","href":"mcdonalds.com","icon":"mcdonalds.com"}]
要获取我使用的最后一项:
To get the last item I use this:
// this is how I parse the arrays
var result = JSON.parse(localStorage.getItem("result"));
for(var i=0;i<result.length;i++) {
var item = result[i];
$('element').val(item.href);
}
如何获取第3项或特定ID的href?
推荐答案
jQuery为此提供了过滤器帮助程序:
jQuery has filter helper for that:
$(result).filter(function(){return this.id == "item-3";})[0]
具有特定ID的商品的href功能如下:
Function for href of item with specific id would be:
function getItemHrefById(json, itemId){
return json.filter(function(testItem){return testItem.id == itemId;})[0].href;
}
示例用法是:
var href = getItemHrefById(result, "item-3");
您可以在 http://jsfiddle.net/LXvLB/
更新
如果您无法从本地存储中读取项目,则可能是在设置值时忘记了调用JSON.stringify:
If you cannot read item from local storage, maybe you forgot to call JSON.stringify when setting value:
localStorage["results"] = JSON.stringify([{"id":"item-1","href":"google.com","icon":"google.com"},
{"id":"item-2","href":"youtube.com","icon":"youtube.com"},
{"id":"item-3","href":"google.com","icon":"google.com"},
{"id":"item-4","href":"google.com","icon":"google.com"},
{"id":"item-5","href":"youtube.com","icon":"youtube.com"},
{"id":"item-6","href":"asos.com","icon":"asos.com"},
{"id":"item-7","href":"google.com","icon":"google.com"},
{"id":"item-8","href":"mcdonalds.com","icon":"mcdonalds.com"}])
您需要将json转换为字符串以正确序列化(并使用JSON.parse取回JSON)
You need to convert json to string to be properly serialized (and to use JSON.parse to get JSON back)
这是最终示例.
编辑
正如无用代码所指出的那样,这种方法比本地过滤器功能(和自定义循环)要慢得多(但我认为,除非出现性能问题,否则引入一些新的代码行来节省20-30ms是过大的),所以我更新我的示例以不使用jquery过滤器.请为此+1.
As Useless Code pointed out, this metod is substantially slower than native filter function (and custom loop but I think that introducing few new lines of code to save 20-30ms is overkill unless performance is a issue), so I'm updating my example to not use jquery filter. +1 please for his answer for that.
此外,在这里需要指出的重要一点是,如果此数组将具有数百个而不是8个书签,那么从统计学上来说for循环可能快两倍左右(因为它不必遍历数组的其余部分).但是,在那种情况下,最好将for循环放到函数中,该函数返回满足条件的第一个找到的项,并且使用 prototypejs 它甚至可以连接到数组.
Also, what is important to point out here, if this array would have hundreds instead of 8 bookmarks, for loop would probably be statistically about twice faster (as it does not have to iterate through rest of the array). But, in that case it would probably be a good idea to put for loop into function which returns first found item which satisfies condition, and with prototypejs it probably can even be hooked up to array.
这篇关于localstorage:获取特定的localstorage值,其中包含许多项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!