0

I am trying to build arrays within an array and I would like to be able to access the stored data through array[x][y]. I have a sample code below that I can't get to work the way I want and I wonder if anyone can spot the problem.

$.getJSON("/data/", function(data){

    var test = {};
    var levels = [1,2,3];

    $.each(data, function(key, value){

        var cat = data[key];

        $.each(levels, function(li,l){

            var base = cat[2];
            var keys = Object.keys(base);

            if (l == 1) {

                test[l] = {};

                $.each(keys, function(ki,k){
                    test[l][k] = base[k][0];
                });
            }

        });

    });

    alert(Object.keys(test[1]));

});

The structure of the fetched JSON data is as shown here:

{
    "1": ["Distribution", 0,
    {
        "9": ["Salmonella", 0,
        {}],
        "6": ["E. coli", 0,
        {
            "8": ["from pigs", 0,
            {}],
            "7": ["from humans", 0,
            {}]}]}],
    "2": ["Adhesin", 0,
    {
        "1": ["PapG", 1,
        {
            "2": ["has three subtypes:", 0,
            {
                "3": ["PapG-I", 0,
                {}],
                "4": ["PapG-II", 0,
                {}],
                "5": ["PapG-III", 0,
                {}]}]}]}],
    "3": ["Receptors", 0,
    {}],
    "4": ["Disease associations", 0,
    {}],
    "5": ["Regulation", 0,
    {}]
}​

The code above doesn't seem to store values to the array within the test array as alert(Object.keys(test[1])) doesn't give anything (i.e. just blank alert).

Edit: after the mistakes spotted by Oleg V. Volkov (below):

$.getJSON("/data/", function(data){

        var test = {};
        var levels = [1,2,3];

        $.each(data, function(key, value){

               // var cat = data[key];

               $.each(levels, function(li,l){

                     var base = value[2];
                     var keys = Object.keys(base);

                     if (l == 1) {

                          if ($.inArray(l,test) == -1){ 
                              test[l] = {};
                          }

                          $.each(keys, function(ki,k){
                             test[l][k] = base[k][0];
                          });
                      }

               });

        });

        alert(Object.keys(test[1]));

});

Still not working.

8
  • your JSON seems wrong formatted! Commented May 12, 2012 at 9:48
  • @TheSystemRestart I think this is Python dictionary. Commented May 12, 2012 at 9:54
  • @VisionN - Right. I corrected it now to show the JSON format. Commented May 12, 2012 at 9:59
  • I recommend to set breakpoints in your code, go through it step by step and inspect the variables. Commented May 12, 2012 at 10:15
  • @FelixKling - I have been doing it. So far, I can't pin down where it's going wrong. A few more tries needed. Commented May 12, 2012 at 10:36

1 Answer 1

1
var cat = data[key];

You don't need this line. Your data is already in value.

You're overwriting same test[l] values on each iteration, so in the end you will only have result of the last outermost iteration in it - in many browsers that will be one that goes over "5": ["Regulation", 0, {}] key/value pair. Quick look over you code suggest cat[2] will be {} for it and that so last iteration will overwrite anything in test[] with empty objects. So your keys correctly returns empty list, but you have an error in your logic.

Regarding your updated example: You're still overwriting test entries with {} because you use .inArray to check it and it will always succeed in not finding any number value in array. .inArray looks for a specific value, not keys/indexes. You should check if specific index is empty instead with if(!test[l])

Sign up to request clarification or add additional context in comments.

3 Comments

I edited the code (shown above) taking into account the mistakes you have observed. Still not working.
What exactly do you want to do with your data? Intent of your code is not exactly clear to me.
The data is hierarchical with tree-like structure. In every level in the hierarchy, I want to store a chunk of info about each member into a single associative array. Each member has a unique id. In the end, I want to be able to access each chunk through array[level][member_id]. The code above does not exactly do what I want to do. It is just oversimplified to replicate the issues I'm experiencing.

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.