Jump to content

For(X In Obj)


Drycodez

Recommended Posts

I know about for in loop and what it does, but my prob is that: i don't knw why there's a variable (x) inside the for-in-loop, followed by 'in'. I just dont knw how to Xplain my prob, but the stuff i really confusing!person={name:'o.s'} For(x in person)//oscan some one please explain DIS confusing for-in-loop?

Link to comment
Share on other sites

well, if you alerted x, you would get the key(s) of an object, in this case, name. If you alerted person[x], you would get o.s. The convenience provided by this loop is that it allows you to iterate through an object without having to know it's keys. It's not like an array where you can index its keys numerically. The reason x is used is just like in a regular loop, it holds the current key/index that it is being iterated on.

for(var i = 0; i < array.length; i++){  console.log(array[i]);};

same concept.

Link to comment
Share on other sites

In the for...in loop, the lefthand operand of the in operator is a memory space that takes the name of one of the properties of the righthand operand on each iteration.

var x;  var obj = {  "a" : 1,  "b" : 2};// Begin a trace:for(x in obj) {  console.log("x = " + x);  console.log("obj." + x + " = " + obj[x]);}

The output will look like this:x = aobj.a = 1x = bobj.b = 2

Link to comment
Share on other sites

am I missing something SM? I don't see what is wrong with Ingolme's example... I'm not sure where you got the square brackets from.... unless mods can edit their posts without an edited post message showing.

Link to comment
Share on other sites

Most browsers today support the console.log() method. It just writes to the browser's Javascript console, or to Firebug. It's great for debugging because it doesn't modify the page and it's less bothersome than an alert dialog.

Link to comment
Share on other sites

try it. it's an alternative to using alert so that you can effectively trace and track the logic of your applications. It's cross browser compatible, even in IE (7+). In my experience getting it to work in IE7, I've just made sure to declare it globally. I include it in my main script file for all my webapp's just to be on the safe side.

if (typeof console === 'undefined') {  window.console = {};}; if (!console.log) {  console.log = function () {};  console.log('INIT console logging');};

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...