1

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.

3
  • I've never seen creating a two-dimensional array in JavaScript this way. Commented May 25, 2015 at 17:50
  • I used following reference stackoverflow.com/questions/2808926/… Commented May 25, 2015 at 17:50
  • 1
    You've missed the line photos[a] = []; ; ). Commented May 25, 2015 at 17:52

3 Answers 3

4

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
    };
});
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.