我正在使用Firefox SDK加载一个插件。在插件中,我存储了一个带有简单存储的关联数组。
关联数组我用简单的首选项存储
var spammer = [];
spammer['id'] = res[0];
spammer['username'] = res[1];
spammer['date'] = res[2];
ss.storage.spammers.push(spammer);
console.log(ss.storage.spammers[i]['id']);
console.log(ss.storage.spammers[i]['username']);
console.log(ss.storage.spammers[i]['date']);
这似乎有效,而附加组件处于活动状态时会记录正确的值。
然而,当我在浏览器中运行以下代码时
require("sdk/tabs").on("ready", function(tab) {
for (i = 0; i < ss.storage.spammers.length; i++) {
console.log(ss.storage.spammers[i]['id']);
console.log(ss.storage.spammers[i]['username']);
console.log(ss.storage.spammers[i]['date']);
}
});
但是现在当我运行代码时,运行“未定义”值会被记录
(前3个行是使用simple-prefs存储的前一个会话,3个工作行来自新会话)
是不可能将关联数组存储为简单存储,还是其他问题?
答案 0 :(得分:1)
在
var spammer = [];
spammer['id'] = res[0];
spammer['username'] = res[1];
spammer['date'] = res[2];
ss.storage.spammers.push(spammer);
console.log(ss.storage.spammers[i]['id']);
console.log(ss.storage.spammers[i]['username']);
console.log(ss.storage.spammers[i]['date']);
将var spammer = [];
更改为var spammer = {};
答案 1 :(得分:0)
我目前正在运行的代码:
if (!ss.storage.spammers)
{
ss.storage.spammers = [];
}
var spammer = {};
spammer.userid = res[0];
spammer.username = res[1];
spammer.date = res[2];
ss.storage.spammers.push(spammer);
console.log(ss.storage.spammers[i].userid);
console.log(ss.storage.spammers[i].username);
console.log(ss.storage.spammers[i].date);