How exactly does a for/in loop work?


#1

I've created the following code and it has been deemed correct by the lesson, although I am still confused by the 'for/in' loop.

How am I able to put in anything I want for the "for(var sas in friends)" portion of the code. Am I declaring a new variable IN the object called "sas"? In this case I picked "sas" for the variable but it looks like I can make it anything as long as I make it consistent.

Just wanting to find out what exactly it's doing there....


https://www.codecademy.com/en/courses/javascript-beginner-en-3bmfN/0/7?curriculum_id=506324b3a7dffd00020bf661#


var friends = {};
    friends.bill = {
        firstName: "Bill",
        lastName: "Sanders",
        number: "(512)-230-1716",
        address: ['1111 Mushy Drive', 'New York', 'NY', '22222']
    }
    friends.steve = {
        firstName: "Steve",
        lastName: "Williams",
        number: "(832)-540-1111",
        address: ['2321 Wannabe Lane', 'Houston', 'TX', '71111']
    }

var list = function(friends){
    for(var qwk in friends){
        console.log(qwk);
    }
}

var search = function(name){
    for(var sas in friends){
        if(friends[sas].firstName === name){
            console.log(friends[sas]);
            return friends[sas]
            }
        
    }
}


#2

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in


#3

@blogjumper15258,

T h e - B a s i c s

An object has one or more properties seperated by a comma-,
Each property consists of a property-key and it's associated VALUE

var nyc = {
       fullName: "New York City",
       mayor: "Bill de Blasio",
       population: 8000000,
       boroughs: 5
      };
nyc -object- S p e c i f i e d

The nyc object has 4 properties seperated by a comma-,
- a fullName property with property-key fullName and it's associated string value of "New York City"
- a mayor property with property-key mayor and it's associated string value of "Bill de Blasio"
- a population property with property-key population and it's associated number value 8000000
- a boroughs property with property-key boroughs and it's associated number value 5

for - in - loop - e x p l a i n e d

With the for-in-loop you have a Method
which will iterate over all properties of a given object.
At each iteration it will assign the property-key as a string
to a variable name of your choice.
Thus for (var x in nyc)
will lead to 4 iteration's
iteration-1 var x = "fullName";
iteration-2 var x = "mayor";
iteration-3 var x = "population";
iteration-4 var x = "boroughs";
As they want you to display the property-key and NOT it's associated VALUE
you would use
console.log( x );

If they wanted you to display the associated VALUE of the propery-key you would use
console.log( nyc[x] );

Reference

google search
== the Book ==
javascript [your question] site:developer.mozilla.org

== discussions / opinions ==
javascript [your question] site:stackoverflow.com

== guidance ==
www.crockford.com
http://javascript.crockford.com/code.html <---JavaScript code-convention
http://javascript.crockford.com/survey.html
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
www.developer.mozilla.org/en-US/docs/Web/JavaScript/Guide
http://stackoverflow.com/questions/9549780/what-does-this-symbol-mean-in-javascript
http://stackoverflow.com/questions/3755606/what-does-the-exclamation-mark-do-before-the-function
http://stackoverflow.com/questions/6260475/how-to-the-operator
http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname?rq=1


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.