0

I'm trying to loop throught all my current localStorage items, but I cannot get to make it work properly, always last item is missing, not sure what else it could be.

function sortObject() {

    for(let i = 0; i < localStorage.length; i++) {
        if(localStorage.getItem(i) !== null) {

        let retrieveData = localStorage.getItem(i);
        let retrieveObject = JSON.parse(retrieveData);
        
        console.log(retrieveObject.index);
        }
    } 
}

sadas

Output: 
SortObject()
1
undefined
  • 1
    your keys are `1` and `2` but your loop is `0` and `1` should be `localStorage.getItem( localStorage.key( i ) );` – pilchard May 22 '22 at 22:17
  • Thank you for your help @pilchard, sadly link you proposed as already answered. Doesn't work for me. –  May 22 '22 at 22:30
  • 1
    The duplicate has 13 answers with hundreds of up votes, surely one of them 'works' – pilchard May 22 '22 at 22:33

0 Answers0