我有以下组件:
patientDiseasesStorage = new Object()
patientDiseasesStorage['p158246547'] = [1, 3, 8, 2, 5] //and many more of this with different p-number
我现在尝试保存此对象/数组组合
localStorage.setItem('patientDiseasesStorage', JSON.stringify(patientDiseasesStorage));
但是,当我尝试从localStorage读回它时,它没有正确的值:
patientDiseasesStorage = JSON.parse(localStorage.getItem('patientDiseasesStorage'));
patientDiseasesStorage['p158246547'] is now undefined and not the array.
我究竟做错了什么?
最佳答案
如果p158246547
是字符串而不是变量名,则应在其周围加上引号:
patientDiseasesStorage['p158246547'] = [1, 3, 8, 2, 5] //and many more of this with different p-number
将其从localStorage中拉出时,也要使用引号。