【问题标题】:jQuery TypeError: array index is undefinedjQuery TypeError:数组索引未定义
【发布时间】:2015-08-07 05:46:00
【问题描述】:

我有一些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

我该如何解决这个问题。顺便问一下,有没有优化的方法?提前致谢。

【问题讨论】:

标签: javascript jquery arrays


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    当你这样做时

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

    【讨论】:

      【解决方案3】:
      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++;
              });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-08
        • 1970-01-01
        • 2014-06-21
        • 2015-12-15
        • 2011-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多