Loop & search through ALL items in localStorage

后端 未结 2 1430
无人共我
无人共我 2021-01-04 06:47

I\'m trying to loop through localStorage to get ALL items through localStorage.length that works with my search algorithm. If i change: i < localStorag

相关标签:
2条回答
  • 2021-01-04 07:18

    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
    }
    
    0 讨论(0)
  • 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).

    0 讨论(0)
提交回复
热议问题