javascript - 从 localStorage 中检索数据并将其显示在表中
问题描述
我正在尝试从 localStorage 检索数据并展示表中的所有条目,但我无法访问密钥。
最后console.log
显示特定条目。myFunction()
console.log
显示所有条目,但是当我尝试return
所有条目时,Cannot read property 'KlientoNr'
出现错误。在第二个<td>
我试图直接访问条目,但我得到undifiend
. 如何正确显示表格的所有条目?
jQuery(document).ready(function($) {
const clients = JSON.parse(localStorage.getItem("data"));
const odontologas = clients.Odontologas;
const myFunction = () => {
const arrayLength = odontologas.length;
for (var i = 0; i < arrayLength; i++) {
console.log(odontologas[i].KlientoNr);
}
return odontologas[i].KlientoNr;
};
$.each(odontologas, function() {
$("#odontologas").append(`<tr>
<td>${myFunction()}</td>
<td>${odontologas.EilėsNr}</td>
<td>
<button type="submit" class="btn btn-success">
Aptarnautas
</button>
</td>
</tr>`);
});
console.log(odontologas[7].KlientoNr);
});
解决方案
的问题${odontologas.EilėsNr}
是odontologas
整个数组并且没有属性EilėsNr
使用传递给每个回调的参数来访问特定的对象实例
$.each(odontologas, function(i, item) {
$("#odontologas").append(`<tr>
...
<td>${item.EilėsNr}</td>
// OR
<td>${odontologas[i].EilėsNr}</td>
...
推荐阅读
- javascript - TypeError:无法读取未定义的 toString() 属性
- html - 位置为 Sticky 和 100% 宽度的 Css 网格和导航栏
- python - 用 BeautifulSoup 刮新闻
- python - 如何控制 Edge 网络浏览器
- java - 实例化父对象会引发错误
- java - WindowsBuilder 无法显示 GUI
- arrays - 为什么程序没有输入就终止?
- java - 尽管编译了运行器文件,但 GUI 不会出现
- android - 如何根据进度百分比设置垂直进度条颜色?
- c++ - 为什么 GCC 和 MSVC 不基于未初始化的变量进行优化?我可以强迫他们吗?