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
  • 25,496
  • 15
  • 91
  • 179
Jonathan
  • 2,953
  • 3
  • 26
  • 37
  • perhaps this SO answer would be useful to you: http://stackoverflow.com/questions/3138564/looping-through-localstorage-in-html5-and-javascript – sv_in Apr 10 '12 at 04: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). – Ramesh Apr 10 '12 at 04:53
  • 2
    Please use `var i` to avoid creating a global variable (which is extremely bad, especially for a loop variable) – ThiefMaster May 26 '12 at 17:19

3 Answers3

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
}
user2428118
  • 7,935
  • 4
  • 45
  • 72
  • Aren't objects and arrays expected to behave same in context i.e accessing the values by array or object syntax – Varinder Singh Jul 07 '13 at 15:36
  • @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? – user2428118 Aug 17 '13 at 13:59
  • 1
    Yes, `localStorage` does posses `length` property.So,As per the problem in question `localStorage` items could be looped through using trivial `for()` – Varinder Singh Aug 19 '13 at 17:38
  • Looping through all data in localstorage may bring overhead for your app if it is running on client side – CY5 Mar 29 '15 at 15:23
  • localStorage and window.localStorage are the same http://stackoverflow.com/questions/12660088/is-there-any-difference-between-window-localstorage-and-localstorage – Jason Crosby Jul 29 '16 at 14:52
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).

Jonathan
  • 2,953
  • 3
  • 26
  • 37
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));
   }
}
cuixiping
  • 24,167
  • 8
  • 82
  • 93