building multi dimensional array

I am trying to build a multi dimensional array with node.js using redis as the data source. I am however not doing it right.

the menuKey contains key containing a sorted set in redis, the sorted set contains values referencing other keys. e.g:

menu:main:sections contains ["menu:main:section1","menu:main:section2"]
menu:main:section1 contains ["option1", "option2"]
menu:main:section2 contains ["option1"]

the array I am trying to build:

[["option1", "option2"], ["option1"]]

This is the code I have but I am placing a callback wrong somehow?

function handleMenu(jsonrpc) {
  var params = jsonrpc['params'];
  var result = [];
  var sections = [];

  menuKey = 'menu:' + params['menu'] + ':sections';
  async.series([
      function (callback) {
        redis.zrevrange(menuKey, 0, -1, function(err, sections) {
          async.forEachSeries(sections, function(section, sectionCallback) {
            redis.zrevrange(section, 0, -1, function(err, items) {
              result.push(items); 
              sectionCallback();
            });
          }, callback);
        });
      }
  ], function() {
    console.log(result);
  });
}

I don't see what I am doing wrong, please advice.

I think you have two problems.

  1. you need a javascript object instead of an array as your top level container
  2. you appear to be overwriting your results at the end

Try this:

function handleMenu(jsonrpc) {
  var params = jsonrpc['params'];
  var result = {}; // THIS LINE IS DIFFERENT
  var sections = [];

  menuKey = 'menu:' + params['menu'] + ':sections';
  async.series([
     function (callback) {
         redis.zrevrange(menuKey, 0, -1, function(err, values) {
         sections = values;
         callback(); 
     });
  },
  function (callback) {
    async.forEachSeries(sections, function(section, cb) {
      redis.zrevrange(section, 0, -1, function(err, values) {
        result[section] = values;
        cb();
      });
    }, callback);
  }
  ], function() {
    // result = sections; // <--- DON'T DO THIS
    console.log(result);
  });
}

In this case result will look like this:

{"menu:main:section1": ["option1", "option2"], "menu:main:section2": ["option1"]}

You don't need a two dimensional array. sections is an ordered index into result. You go through sections in order and use these keys to extract arrays which preserve your secondary order. Like so:

var i = 0,
    j = 0,
    section,
    contents;

for(i = 0; i < sections.length; i++){
    section = sections[i];
    contents = result[section];
    for(j = 0; j < contents.length; j++{
        console.log(contents[j]);
    }
}

UPDATE:

If absolutely have to have a 2D array. Then declare a variable i (and set it to 0) at the top of handleMenu and use it instead of sections, like this:

result[i++] = values;

also, change the result declaration back to this:

var result = [];