Jump to content

Correct syntax to access elements of an object


wongadob
 Share

Recommended Posts

I have some code that I cannot quite get the right syntax. I have a rather complex objects that is a mix of indexed and associated arrays ( I assume you can mix becuase mostly it works) But I just cannot figure out how to get to my messages array. Here is my line and then I shall explain some more

console.log ('messages - ' + networks[currentNetwork]['messages']);

The array structure is as follows (where I have 0 - zero is an indexed array). I have given 2 examples so that you can see the master array Neworks has numerous entries. Each have new arrays of messages, users etc, then they each have assciative arrays of properties.

Networks[0]['messages'][0]['propertyx']Networks[0]['users'][0]['propertyy]

What I am trying to do with the print is to print the entire sub-object of messages. infact I will need to reference these sub objects frequently. Any help much appreciated

Edited by wongadob
Link to comment
Share on other sites

You can use the for(in) loop to display every single property or method that pertains to an object. An associative array and an object are the same thing in Javascript. A normal for loop will work for numeric arrays, if you want to check the properties of objects that are in a numeric array you'll need a for(in) loop inside a for() loop.

Link to comment
Share on other sites

so as we discussed in your last thread, networks is an (numeric) array, since you are using array methods on it (push) and not an object, so this will not work

console.log ('messages - ' + networks[currentNetwork]['messages']);

you need to iterate over the numeric array, so you can access each index, which in this case, is a network object. From there, you can test each property, in this case name, to determine if it's the currentNetwork you are looking for.

var currentNetworkObj = {};var messages = []; for(var i = 0, l =  networks.length; i < l; i++){  var network = networks[i];  if(network.name == currentNetwork){	  currentNetworkObj = network;  };};console.log('Found current network => ' + currentNetworkObj.name); //see the messagesfor(var i = 0, l = currentNetworkObj.messages.length; i < l; i++){  var count = i +1;  var message = currentNetworkObj.messages[i];  console.log('message #' + count + ' => ' + message);}; //example of iterating over an objectfor(var p in currentNetworkObj){  console.log('key => ' + p);  console.log('value => ' + currentNetworkObj[p]);};

Edited by thescientist
Link to comment
Share on other sites

currentNetwork was an index so it does work. I have since found the root of my problem lay elsewhere. The reason it was not outpuuting the object was becuase the object was empty and infact outputting correctly as soon as I populated the array of messages! Doh!

Link to comment
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
 Share

×
×
  • Create New...