wongadob Posted March 4, 2012 Share Posted March 4, 2012 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 Link to comment Share on other sites More sharing options...
wongadob Posted March 4, 2012 Author Share Posted March 4, 2012 This may also help to explain the structure networks.push ({name: Profile.network.name, info: Profile.network, user: Profile.user, messages: new Array(), users: new Array() }); Link to comment Share on other sites More sharing options...
eTianbun Posted March 4, 2012 Share Posted March 4, 2012 If your aim is to print the properties of each indexed object i dont see any reason why you are assigning names to the properties: Why not something like this: [messages][0][0] [index] the properties! Link to comment Share on other sites More sharing options...
Ingolme Posted March 4, 2012 Share Posted March 4, 2012 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 More sharing options...
thescientist Posted March 4, 2012 Share Posted March 4, 2012 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]);}; Link to comment Share on other sites More sharing options...
wongadob Posted March 4, 2012 Author Share Posted March 4, 2012 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 More sharing options...
thescientist Posted March 4, 2012 Share Posted March 4, 2012 ah, I guess it wasn't clear then what the value of currentNetwork was. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.