Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在localstorage中保留对象数组_Javascript_Arrays_Json_Object_Local Storage - Fatal编程技术网

Javascript 如何在localstorage中保留对象数组

Javascript 如何在localstorage中保留对象数组,javascript,arrays,json,object,local-storage,Javascript,Arrays,Json,Object,Local Storage,我正在尝试将localstorage用于我的对象数组,但如果没有localstorage,它将无法正常工作。经过几天的尝试,我在这里尝试。有人能帮忙吗? 守则: <script> var notes = JSON.parse(localstorage.getitem("notes") || "[]"); function todo() { // Gets invoked by a submit

我正在尝试将localstorage用于我的对象数组,但如果没有localstorage,它将无法正常工作。经过几天的尝试,我在这里尝试。有人能帮忙吗? 守则:

  <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>

您使用了不正确的关键字

localstorage=>localstorage


getitem=>getitem

除了一些语法错误之外,您的代码似乎有效。使用一个好的IDE或代码编辑器或任何东西来显示错误。我推荐


您需要localStorage.getItem而不是localStorage.getItem吗?与大多数其他语言一样,JavaScript与许多JavaScript属性和函数一样区分大小写。
try please declare as array, note variable

 var note = array();
             note = {
                list: list,
                taskdate: taskdate,
                tasktime: tasktime
            }
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));
}