jQuery multidimensional object with multidimensional arrays

Your datasets is an object, not an array - you can't push to it. Also, your variable categoryName does not work like that (read more on dot and bracket notation). Try this:

var datasets = {};
$('div#Container ul').each(function () {
    catName = this.id;
    var array = [];
    $('li', this).each(function () {
        array.push([
          new Date($(this).data('key')).getTime(),
          $(this).text()
        ]);
    });
    datasets[catName.toLowercase()] = {
        label: catName.toUppercase(),
        data: array
    };
});

Also, I'm not sure whether you really would need to create Date objects, but that depends on your in- and output format.


Change var objects = []; to var objects = {}; and change

objects.push({ categoryName: { label: categoryName, data: array} });

to

objects[categoryName] = { label: categoryName, data: array};

The problem you have with JSON objects is in setting properties with a variable index. You can use array notation to do that, as above.

Tags:

Jquery

Flot