Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7942681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:02:58+00:00 2026-06-04T00:02:58+00:00

I am trying to build arrays within an array and I would like to

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:02:59+00:00Added an answer on June 4, 2026 at 12:02 am
    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])

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build an array within The Loop of values provided from various
I'm trying to build an associative array in PHP dynamically, and not quite getting
I am trying to build a hierarchical array in PHP, from relational database contents
I'm trying to build up a string array in JavaScript and get the results
I am trying to build a program that accepts an array of integers as
Trying to build Xuggler under Windows. Xuggler is core native code functions wrapped into
I'm trying to build a user based message system. I'd like messages to show
I am trying to build a 3D Javascript array, but I am unsure of
I'm trying to build a multidimensional array dynamically. What I want is basically this
I'm trying to build a simple application, with the finished program looking like this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.