Pętla i wyszukiwanie WSZYSTKICH przedmiotów w localStorage
Próbuję przejść przez localStorage, aby uzyskać WSZYSTKIE elementylocalStorage.length
to działa z moim algorytmem wyszukiwania. Jeśli zmienię:i < localStorage.length
wewnątrz pętli for po prostu liczby, tj .:for (i=0; i<100; i++)
zamiast:(i=0; i<=localStorage.length-1; i++)
, wszystko Prace. Jednak zdaję sobie sprawę, że problem może leżeć w algorytmie wyszukiwania.
Kod pobierający wszystkie elementy:
<code> 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 } </code>
Mój działający (!?) Algorytm wyszukiwania:
<code> 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; } } </code>
Co jest nie tak z moimlocalStorage.length
lub co jest nie tak z algorytmem wyszukiwania?