andDev
andDev

Reputation: 275

jQuery TypeError: array index is undefined

I have some divs and need to store data of each div in a array. So i used a two dimensional array to store these data in following way.

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

But i get following error in firebug when i try to store more than one div in array

TypeError: noteList[count] is undefined

How can i solve the problem. By the way, is there any optimized way? Thanks in advance.

Upvotes: 1

Views: 1540

Answers (3)

dfsq
dfsq

Reputation: 193301

The way you create "two-dimentional" array is not optimal for this task. You would better use an array of objects which you can create like this:

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

However what you really need is to use $.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
    };
});

Upvotes: 4

hgoebl
hgoebl

Reputation: 13007

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

Upvotes: 0

AmmarCSE
AmmarCSE

Reputation: 30607

When you do

var noteList = new Array(new Array());

you have an array of arrays. This is fine, but you cannot set property values as if you were using an object. Instead of

noteList[count]['content'] = content;

you would have to do

noteList[count].push(content);

If you wish to push objects, you can do

var myObj = {
'content' : content,
'properties' : properties,
'noteID' : noteID
};
noteList[count].push(myObj);

Upvotes: 2

Related Questions