简体   繁体   English

如何将对象添加到localStorage中的数组

[英]how to add objects to array in localStorage

I tried the solution posted on this [ adding objects to array in localStorage but it doesnt seem to work for me.Mine has a small change,I am getting the values as a list from python flask app: 我尝试了在此发布的解决方案[ 在localStorage中将对象添加到数组中,但似乎对我不起作用。我的变化很小,我从python flask应用程序中以列表的形式获取值:

    var myValue = {{ List|safe }};
    onload=function appendRow() {
    var names = [];
    names = myValue;
    var existingEntries = JSON.parse(localStorage.getItem("allEntries")) || [];
    localStorage.setItem("oltbl", JSON.stringify(names));
    existingEntries.push(oltbl);
    localStorage.setItem("allEntries", JSON.stringify(existingEntries))
    var storedNames = JSON.parse(localStorage.getItem("allEntries"));
    var tbl = document.getElementById('mixed'), 
    row = tbl.insertRow(tbl.rows.length),     
    i;
    for (i = 0; i < tbl.rows[0].cells.length; i++) {
    createCell(row.insertCell(i), storedNames[i], 'row');
     }
     }

    function createCell(cell, text, style) {
    var div = document.createElement('div'), 
    txt = document.createTextNode(text); 
    div.appendChild(txt);                    
    cell.appendChild(div);                  
    }
    </script>
    <body>
    <table id="mixed">
    <tr>
    <th>Title</th>
    <th>Text</th>
    <th>STATUS</th>
    </tr>

Any pointers? 有指针吗?

You're always setting the allEntries the result of itself, but since you never set a thing to it, it's blank 您总是将allEntries设置allEntries自身的结果,但是由于您从未为它设置任何内容,因此它是空白的

You should rethink what exactly what you want to achieve. 您应该重新考虑您想要实现的目标。

also

existingEntries.push(oltbl); /*oltbl IS NOT ASSIGNED*/

and causes js error 并导致js错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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