jimfog Posted July 28, 2011 Share Posted July 28, 2011 the following is a code to loop through properties and values of an object-along with the object properties and values: var object={propertya:1,propertyb:2};for(var John in object ){ console.log("name:" + John);var x=object[John];console.log("value:" + x);}The code works but some questions arise.I cannot understand why or how the javascript interpreter relates the var John with the property names and consequently with the property values.In fact, whatever var i define in the for statement inside the parentheses, this, will be related with the property names of the object, namely propertya and propertyb.I hope i was clear. Link to comment Share on other sites More sharing options...
jeffman Posted July 28, 2011 Share Posted July 28, 2011 Traditional for-loops are typically used to iterate through numerically-indexed arrays. They cannot be used in that way for associative arrays or objects. In fact, there is no other way to iterate through the elements of an object without explicitly naming the indexes. That would make for long, clunky code, and make it impossible to iterate through an object if you don't already know the names of the indexes.So a few languages have developed a special looping structure that does iterate through an object, one element at a time. I'm pretty sure a for-loop was chosen because this kind of iteration (with numbers) is what for-loops normally do. But they could have called it anything, really.As to how, the interpreter knows what kind of object you have and what the names of its elements are. So it's no trouble to iterate though the list of them. Link to comment Share on other sites More sharing options...
jimfog Posted July 29, 2011 Author Share Posted July 29, 2011 As to how, the interpreter knows what kind of object you have and what the names of its elements are. So it's no trouble to iterate though the list of them.So, you are saying, that whatever variable name a give, it does not matter because the interpreter already knowsthe property names.Correct?It still though,is, a weird syntax-javascript, sometimes is weird in its syntax. Link to comment Share on other sites More sharing options...
jeffman Posted July 29, 2011 Share Posted July 29, 2011 So, you are saying, that whatever variable name a give, it does not matter because the interpreter already knows the property names.As long as the variable names a defined object, yes. Link to comment Share on other sites More sharing options...
jimfog Posted July 30, 2011 Author Share Posted July 30, 2011 As long as the variable names a defined object, yes.Ok, a have one last question though.What kind of application would require code for looping through properties of an object.From your experience, where it was needed to loop through an object.Thanks. Link to comment Share on other sites More sharing options...
thescientist Posted July 30, 2011 Share Posted July 30, 2011 just about any application can have a practical use for looping through objects. Objects are used a lot in modern programming, especially given the precedent for most developers to program in an OOP convention. While javascript doesn't support true OOP, it does have objects, and it's functional nature can be cast to work similar to classes, even being able to create private variables. Once you start programming more and more, you will find yourself using objects a lot. They are such a simple container for data, and it's more practical to store, say user information in an object than an array. var Person = function(first, last){ this.fname = first; this.lname = last; return this;};var doug = Person('doug','benson'); from there though, you could create hundeds of Person objects, and to contain them all, you could put all the objects in an array, which is what I usually do. that way you could loop through all your people objects, and then print out their names. //assuming people is an array of Person objectsfor(var i = 0, l = persons.length; i < l; i++){ console.log('Hi ' + persons[i].fname + ' ' + persons[i].lname);}; personally, I don't find myself looping through objects nearly as much in JS because of the easily accessible nature of objects, as I would say in PHP, where I find myself dealing with associative array's more often. Link to comment Share on other sites More sharing options...
Ingolme Posted July 30, 2011 Share Posted July 30, 2011 just about any application can have a practical use for looping through objects. Objects are used a lot in modern programming, especially given the precedent for most developers to program in an OOP convention. While javascript doesn't support true OOP, it does have objects, and it's functional nature can be cast to work similar to classes, even being able to create private variables. Once you start programming more and more, you will find yourself using objects a lot. They are such a simple container for data, and it's more practical to store, say user information in an object than an array.var Person = function(first, last){ this.fname = first; this.lname = last; return this;};var doug = Person('doug','benson'); from there though, you could create hundeds of Person objects, and to contain them all, you could put all the objects in an array, which is what I usually do. that way you could loop through all your people objects, and then print out their names. //assuming people is an array of Person objectsfor(var i = 0, l = persons.length; i < l; i++){ console.log('Hi ' + persons[i].fname + ' ' + persons[i].lname);}; personally, I don't find myself looping through objects nearly as much in JS because of the easily accessible nature of objects, as I would say in PHP, where I find myself dealing with associative array's more often. But that example isn't looping through the properties of an object. I've never had a good reason to loop through the properties of an object because each property has its own use.There's only one good reason to loop through the properties of an object, and that's to clone the object. Link to comment Share on other sites More sharing options...
thescientist Posted July 30, 2011 Share Posted July 30, 2011 yeah, in retrospect I guess I only find myself doing that in PHP, where the simplest object like type (without having to make a class) there is, is really just an associative array. Link to comment Share on other sites More sharing options...
justsomeguy Posted July 31, 2011 Share Posted July 31, 2011 You would really only need to loop through properties if you're doing a custom object copy or something like that. Link to comment Share on other sites More sharing options...
larrybeasley Posted September 23, 2011 Share Posted September 23, 2011 I did this by accident. For my class I was to make a javascript program based on a music store. Here is how I implemented the code.MusicChoice = {};MusicChoice.artist="";MusicChoice.media="";MusicChoice.quantity=0;MusicChoice.lineitem=0;runningtotal=0;MusicChoice = [];MusicChoice.push("Bob Dylan" , "DVD", "1" , "15");//To displaymyiFrame.document.write('<table width="780" border="0" cellpadding="5"> ');myiFrame.document.write('<tr><td><em>Artist Name</em></td><td><em>Media Type</em></td><td><em>Quantity</em></td><td><em>Total</em></td></tr>');myiFrame.document.write("<tr>");var j=1;var dSign="";for (index=0; index<MusicChoice.length; index++){ if (j % 4 == 0) { dSign="$";} else {dSign="";}myiFrame.document.write("<td>" + dSign + MusicChoice[index].toString() + "</td>"); if (j % 4 == 0) { myiFrame.document.write("</tr><tr>"); } j++;}I thought I had mutated the object as there was no clearly defined way to declare a var as an array of a user-defined object type.When I punched the code "MusicChoice = [];" into Dreamweaver, it saw the object and give it array functionality. Without it, it treated it like a user-defined object only. I their documentation that this has been done before? Link to comment Share on other sites More sharing options...
thescientist Posted September 23, 2011 Share Posted September 23, 2011 that's because when you wrote MusicChoice = []; you overwrote everything here: MusicChoice = {};MusicChoice.artist="";MusicChoice.media="";MusicChoice.quantity=0;MusicChoice.lineitem=0; Link to comment Share on other sites More sharing options...
justsomeguy Posted September 23, 2011 Share Posted September 23, 2011 You're just using a normal array there. You define it as an object first, but later you define it as an array which completely overwrites the original value. It's not that it gets "mutated", it was a generic object and now it's a new array. The array is not based on the old object, it's new. If you wanted to do an array of objects that would look like this: var MusicChoice = []; MusicChoice.push({ artist: 'Bob Dylan', media: 'DVD', quantity: 1, line: 15}); MusicChoice.push({ artist: 'Frontline Assembly', media: 'DVD', quantity: 5, line: 10}); for (index=0; index<MusicChoice.length; index++){ alert(MusicChoice[index].artist);} Link to comment Share on other sites More sharing options...
larrybeasley Posted September 23, 2011 Share Posted September 23, 2011 Cool Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.