简体   繁体   中英

How to keep array of objects in localstorage

i'm trying to use localstorage for my array of objects but it doesn't work (without the localstorage it works perfectly). after a few days of tries i'm trying here. can anyone help please? The Code:

  <script>
        var notes = JSON.parse(localstorage.getitem("notes") || "[]");        
        function todo() { // Gets invoked by a submit button        
            var list = document.getElementById("tasklist").value;        
            var taskdate = document.getElementById("taskdate").value;        
            var tasktime = document.getElementById("tasktime").value;        
            
            const note = {
                list: list,
                taskdate: taskdate,
                tasktime: tasktime
            }
            notes.push(note);
            for (i = 0; i < notes.length; i++) {
                document.getElementById("name").append(notes[i].list);
            }
            localStorage.setItem("notes", JSON.stringify(notes));
        }
    </script>

You have used incorrect keyword.

localstorage => localStorage

getitem => getItem

Your code seems valid except for some syntax errors. Use a good IDE or code editor or whatever to show you that errors. I recommend vscode

var notes = JSON.parse(localStorage.getItem("notes") || "[]");
function todo() { // Gets invoked by a submit button        
  var list = document.getElementById("tasklist").value;
  var taskdate = document.getElementById("taskdate").value;
  var tasktime = document.getElementById("tasktime").value;

  const note = {
    list: list,
    taskdate: taskdate,
    tasktime: tasktime
  };
  notes.push(note);
  for (let i = 0; i < notes.length; i++) {
    document.getElementById("name").append(notes[i].list);
  }
  localStorage.setItem("notes", JSON.stringify(notes));
}
try please declare as array, note variable

 var note = array();
             note = {
                list: list,
                taskdate: taskdate,
                tasktime: tasktime
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM