14

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++), everthing works. However, I do realize the problem might lie in the search algorithm.

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?

brasofilo
26.2k15 gold badges96 silver badges189 bronze badges
asked Apr 10, 2012 at 4:26
3
  • perhaps this SO answer would be useful to you: stackoverflow.com/questions/3138564/… Commented Apr 10, 2012 at 4:47
  • What is the actual length of your localStorage? Try looping through the same number instead of 100. My thought process is, there may be a problem with data stored after 100 (like not in the format you expect). Commented Apr 10, 2012 at 4:53
  • 2
    Please use var i to avoid creating a global variable (which is extremely bad, especially for a loop variable) Commented May 26, 2012 at 17:19

3 Answers 3

16

localStorage is an object, not an array. Try 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
}
answered May 26, 2012 at 17:17
Sign up to request clarification or add additional context in comments.

5 Comments

Aren't objects and arrays expected to behave same in context i.e accessing the values by array or object syntax
@VarinderSingh It appears that although !(localStorage instanceof Array), the localStorage object does have a length property. I didn’t realize this when writing this answer. Is that what you mean?
Yes, localStorage does posses length property.So,As per the problem in question localStorage items could be looped through using trivial for()
Looping through all data in localstorage may bring overhead for your app if it is running on client side
localStorage and window.localStorage are the same stackoverflow.com/questions/12660088/…
1

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).

answered Aug 18, 2012 at 3:02

Comments

0

print all localStorage items

and exclude the internal properties and methods.

for(var key in localStorage){
 if(localStorage.hasOwnProperty(key)) {
 console.log(key + ' : ' + localStorage.getItem(key));
 }
}
answered Nov 11, 2022 at 1:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.