gpt4 book ai didi

javascript - jQuery 类型错误 : array index is undefined

转载 作者:行者123 更新时间:2023-11-30 08:00:01 25 4
gpt4 key购买 nike

我有一些 div 并且需要将每个 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 中遇到以下错误

类型错误:noteList[count] 未定义

我该如何解决这个问题。请问有什么优化的方法吗?提前致谢。

最佳答案

创建“二维”数组的方式对于这项任务来说并不是最佳的。您最好使用可以像这样创建的对象数组:

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
};
});

关于javascript - jQuery 类型错误 : array index is undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30443520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com