i am trying to make a basic notes app using js a i have stored notes in local storage and i am trying to print those note using for in loop everything is fine but i dont know why am i getting extra values like length, getItem, key etc can anyone help
my code
(function () {
for (key in localStorage) {
let notes = document.getElementById("notes")
let value = localStorage.getItem(key)
notes.innerHTML = notes.innerHTML + `${key}: ${value} <br>`
}
})();
>Solution :
localStorage contains key/value pairs similar to a JS object with some built in functions such as setItem and getItem. In order to iterate over everything in localStorage you would have to do it the same way you would with a JS object. Here is one way to do it.
for (const [key, value] of Object.entries(localStorage)) {
console.log(key, value);
}
This would log the keys and values of each item saved in the localStorage