简体   繁体   中英

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:

    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

You should rethink what exactly what you want to achieve.

also

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

and causes js error

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