jQuery TypeError:数组索引未定义
jQuery TypeError: array index is undefined
我有一些divs
,需要将每个div
的数据存储在数组中。因此,我使用二维数组来存储这些数据,方法如下:
this.saveBoardAndNotes = function (board_id, board_name) {
var noteList = new Array(new Array());
var self = this;
var count = 0;
$(".optimal-sticky-notes-sticker-note").each(function(){
// Replace plain urls with links and improve html
var note = $(this);
var content = note.find(".textarea").html();
var properties = JSON.stringify(self.getProperties(note));
var noteID = note.attr("id");
noteList[count]['content'] = content;
noteList[count]['properties'] = properties;
noteList[count]['noteID'] = noteID;
count++;
});
但是当我试图在数组
中存储多个div时,我在firebug中得到以下错误 TypeError: noteList[count] is undefined
我怎样才能解决这个问题?顺便问一下,有没有优化的方法?
您创建"二维"数组的方式对于此任务不是最佳的。你最好使用一个对象数组,你可以这样创建:
var noteList = [];
var self = this;
var count = 0;
$(".optimal-sticky-notes-sticker-note").each(function () {
// Replace plain urls with links and improve html
var note = $(this);
var content = note.find(".textarea").html();
var properties = JSON.stringify(self.getProperties(note));
var noteID = note.attr("id");
noteList[count] = {};
noteList[count]['content'] = content;
noteList[count]['properties'] = properties;
noteList[count]['noteID'] = noteID;
count++;
});
然而,你真正需要的是使用$.fn.map
:
var self = this;
var noteList = $(".optimal-sticky-notes-sticker-note").map(function () {
// Replace plain urls with links and improve html
var note = $(this);
var content = note.find(".textarea").html();
var properties = JSON.stringify(self.getProperties(note));
var noteID = note.attr("id");
return {
content: content,
properties: properties,
noteID: noteID
};
});
当你这么做的时候
var noteList = new Array(new Array());
你有一个数组的数组。这很好,但是您不能像使用对象一样设置属性值。而不是
noteList[count]['content'] = content;
你必须做
noteList[count].push(content)
;
如果您希望推送对象,可以执行
var myObj = {
'content' : content,
'properties' : properties,
'noteID' : noteID
};
noteList[count].push(myObj);
this.saveBoardAndNotes = function (board_id, board_name) {
var noteList = [];
var self = this;
var count = 0;
$(".optimal-sticky-notes-sticker-note").each(function(){
// Replace plain urls with links and improve html
var note = $(this);
var content = note.find(".textarea").html();
var properties = JSON.stringify(self.getProperties(note));
var noteID = note.attr("id");
noteList[count] = {};
noteList[count]['content'] = content;
noteList[count]['properties'] = properties;
noteList[count]['noteID'] = noteID;
count++;
});
相关文章:
- 尝试在PHP中回显输入文本时出现未定义的索引错误
- 传递给 PHP 表单时 JavaScript 动态变量上的未定义索引
- 使用ajax时,php中未定义的索引
- “未定义的索引:itemdetails”,当将JSON对象从JavaScript发送到PHP时
- 未定义的索引-表单不通过javascript将值发送到php代码
- 尝试使用AJAX将JavaScript变量传递给PHP,但得到'未定义的索引'
- angular.js:13424 ReferenceError:在控制器中使用工厂方法时未定义索引
- 通过AJAX访问时,$_POST中出现未定义的索引错误
- PHP的原因注意:未定义的索引:
- PHP未定义的索引(由jquery文件上传引起)
- 如何删除cookie错误注意:PHP上的未定义索引
- 某些浏览器中cookie的未定义索引
- Ajax / PHP 注意:未定义的索引:
- 面对此错误“ 未捕获的引用错误:幻灯片索引未定义 ”在光滑.js
- 0索引未定义或只是缺少
- 用户:索引未定义
- 所选索引未定义
- 无效的列索引未定义.取值范围为[0-8]之间的整数
- 运行函数几次后索引未定义
- jQuery TypeError:数组索引未定义