Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For.. in loop - why does it work?

I am reading the book "JavaScript for web designers" and I've come to this example:

var fullName = {
  "first": "John",
  "last": "Smith"
};
for (var name in fullName) {
  console.log(name + ": " + fullName[name]);
}

The output is:

"first: John"
"last: Smith"

What I don't get is: where do I tell the program to get the string "first" and "last". I mean, cycling the object "fullName", I fail to see how "name" can be related to "first" and "last". I hope this is clear. Can you help? Many thanks!

like image 741
akmur Avatar asked Oct 13 '16 22:10

akmur


People also ask

Why does a for loop work?

A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables.

How does a for in loop work?

A "For" Loop is used to repeat a specific block of code a known number of times. For example, if we want to check the grade of every student in the class, we loop from 1 to that number. When the number of times is not known before hand, we use a "While" loop.

How for in loop works in Python?

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages.

What is a loop and how does it work?

A looploopWhat Does For Loop Mean? For loop is a programming language conditional iterative statement which is used to check for certain conditions and then repeatedly execute a block of code as long as those conditions are met.https://www.techopedia.com › definition › for-loopWhat is For Loop? - Definition from Techopedia is a programming function that iterates a statement or condition based on specified boundaries. The loop function uses almost identical logic and syntax in all programming languages.


3 Answers

for..in iterates over the keys of an object. You can then access an objects values by name using brackets.

var obj = {
  a: 1,
  b: 2,
  c: 3
};

for (var key in obj) {
  console.log('Key:', key);
  console.log('obj[key] == obj["' + key + '"] == obj.' + key);
  console.log('obj.' + key + ' == ' + obj[key]);
}
like image 177
Mike Cluck Avatar answered Oct 09 '22 10:10

Mike Cluck


It's pretty simple to learn and/or understand. You're looping through all of the properties in the object fullName. For each property, you're giving it the temporary name/alias of name

So you could change it to for (var anything in fullName) and then in the body of the for loop you would reference each property by the name anything like so:

for (var anything in fullName) {

    // anything is an alias for the current property your on of the object you're looping through
    console.log(anything + ": " + fullName[anything]);

}
like image 45
Brandon Avatar answered Oct 09 '22 11:10

Brandon


A for..in loop will iterate through the object's keys. If you use this on an array, then it (most browser engines) will convert the array into an object behind the scenes (read more on Dictionary Mode) and iterate the keys.

You can view the keys by using

var fullName = {
  "first": "John",
  "last": "Smith"
};
console.log(Object.keys(fullName));

And essentially the result of this call is then iterated. Keep in mind that using for..in does not guarantee order of the key value pairs.

like image 42
Travis J Avatar answered Oct 09 '22 10:10

Travis J