![](/img/trans.png)
[英]How to add a new object to an array of objects in localStorage?
[英]how to add objects to array in localStorage
我嘗試了在此發布的解決方案[ 在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>
有指針嗎?
您總是將allEntries
設置allEntries
自身的結果,但是由於您從未為它設置任何內容,因此它是空白的
您應該重新考慮您想要實現的目標。
也
existingEntries.push(oltbl); /*oltbl IS NOT ASSIGNED*/
並導致js錯誤
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.