Jump to content
Sign in to follow this  
Squarepusher

How to read names of arrays within an array?

Recommended Posts

Hello,

I'm currently learning JavaScript and I plan to work a lot with arrays. I store all arrays in external files and use JSON to access them. Now I wanted to read the names of all arrays that are nested in one main array to create a navigation list but I don't know how to do that.

 

This is an example of a JSON file I use:

{"MainArray":[
  {"SubArray1":[{
     ...
  }]
  },
  {"SubArray2":[{
     ...
  }]
  },
  {"SubArray3":[{
     ...
  }]
  }
]}

How do I get the names of all SubArrays? I tried this

// arr is a JSON-parsed string
function myFunction(arr) {
  var out = "";
  var i;
  for(i = 0; i < arr.MainArray.length; i++) {
    out += arr.MainArray[i].toString() + '<br>';
  }
  document.getElementById("aso").innerHTML = out;
}

but it only creates a list of "[object Object]" items. Should I create another JSON file where all array names are listed instead?

Share this post


Link to post
Share on other sites

You need a better way to structure your data. Something like this, for example:

{
  name : "MainArray",
  data : [
    {
      name : "SubArray1",
      data : [
        ...
      ]
    },
    {
      name : "SubArray2",
      data : [
        ...
      ]
    },
    {
      name : "SubArray3",
      data : [
        ...
      ]
    }
  ]
}

Share this post


Link to post
Share on other sites

I'm currently learning JavaScript and I plan to work a lot with arrays. I store all arrays in external files and use JSON to access them. Now I wanted to read the names of all arrays that are nested in one main array to create a navigation list but I don't know how to do that.

 

You don't explain why you have this type of data-structure.

Share this post


Link to post
Share on other sites

 

You need a better way to structure your data. Something like this, for example:

{
  name : "MainArray",
  data : [
    {
      name : "SubArray1",
      data : [
        ...
      ]
    },
    {
      name : "SubArray2",
      data : [
        ...
      ]
    },
    {
      name : "SubArray3",
      data : [
        ...
      ]
    }
  ]
}

 

I don't know how to access these array...

 

 

You don't explain why you have this type of data-structure.

 

I want to change content on the page without loading a whole new page. Just like AJAX.

Share this post


Link to post
Share on other sites

Those are objects and arrays. If that structure is stored in a variable called obj, then you can loop through it like this:

 

var name1 = obj.name;
for (var i = 0; i < obj.data.length; i++) {
  var name2 = obj.data[i].name;
  for (var j = 0; j < obj.data[i].data.length; j++) {
    ...
  }
}
If you're going to be using that kind of thing you'll want to look into objects and arrays in Javascript. JSON is just a shorthand notation to create them, you're creating objects and arrays though. You'll need to be familiar with them regardless of the actual structure you eventually use. Objects make it easier because you can name the properties. Those objects have a property called name that is a string and another property called data that is an array.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×