javascript 循环并搜索 localStorage 中的所有项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10083098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loop & search through ALL items in localStorage
提问by Jonathan
I'm trying to loop through localStorage to get ALL items through localStorage.length
that works with my search algorithm. If i change: i < localStorage.length
inside the for loop to simply a number, i.e: for (i=0; i<100; i++)
instead of: (i=0; i<=localStorage.length-1; i++)
, everthingworks. However, I do realize the problem might lie in the search algorithm.
我正在尝试遍历 localStorage 以通过localStorage.length
我的搜索算法获取所有项目。如果我i < localStorage.length
将 for 循环中的: 更改为简单的数字,即:for (i=0; i<100; i++)
而不是: (i=0; i<=localStorage.length-1; i++)
,则一切正常。但是,我确实意识到问题可能出在搜索算法上。
The code getting all items:
获取所有项目的代码:
var name = new Array();
for (var i = 0; i <= localStorage.length - 1; i++) { // i < 100 works perfectly
key = localStorage.key(i);
val = localStorage.getItem(key);
value = val.split(","); //splitting string inside array to get name
name[i] = value[1]; // getting name from split string
}
My working (!?) search algorithm:
我的工作(!?)搜索算法:
if (str.length == 0) {
document.getElementById("searchResult").innerHTML = "";
}
else {
if(str.length > 0) {
var hint = "";
for(var i=0; i < name.length; i++) {
if(str.toLowerCase() == (name[i].substr(0, str.length)).toLowerCase()) { //not sure about this line
if(hint == "") {
hint = name[i];
} else {
hint = hint + " <br /> " + name[i];
}
}
}
}
}
if(hint == "") {
document.getElementById("searchResult").innerHTML=str + " st?r inte p? listan";
} else {
document.getElementById("searchResult").innerHTML = hint;
}
}
What is wrong with my localStorage.length
, or what is wrong with the search algorithm?
我的有什么问题localStorage.length
,或者搜索算法有什么问题?
采纳答案by Jonathan
Problem now SOLVED. The issue was that each time data was saved to localStorage, one extra empty item was stored at the bottom of the local db as a consequence of an incorrectly written for loop (in the setItem part.) arrayIndex < guestData.length should have been arrayIndex < guestData.length-1. arrayIndex < guestData.length-1 stores all items without creating an empty item at the bottom of the database which later messed up the search algorithm, as the last value to be search was undefined (the empty item).
问题现已解决。问题是每次将数据保存到 localStorage 时,都会在本地数据库的底部存储一个额外的空项目,这是由于 for 循环写入错误(在 setItem 部分)。arrayIndex < guestData.length 应该是 arrayIndex < 访客数据.length-1。arrayIndex < guestData.length-1 存储所有项目,而不会在数据库底部创建一个空项目,该项目后来弄乱了搜索算法,因为要搜索的最后一个值是未定义的(空项目)。
回答by user2428118
localStorage is an object
, not an array
.
Try for(var i in window.localStorage)
:
localStorage 是一个object
,而不是一个array
。尝试for(var i in window.localStorage)
:
for(var i in window.localStorage){
val = localStorage.getItem(i);
value = val.split(","); //splitting string inside array to get name
name[i] = value[1]; // getting name from split string
}