Lets be clear, I'm not asking for for(var i in list) I want the key assigned to a variable as well.
Asked
Active
Viewed 1,140 times
2 Answers
8
The key is, actually, the only thing that gets assigned:
var value;
for(var key in list) {
value = list[key];
// do something with key, value
}

Matchu
- 83,922
- 18
- 153
- 160
-
You are right indeed. Why I believed the opposite to be true, nfi. Thanks! – lsl Sep 24 '10 at 02:21
1
Javascript doesn't have this functionality built into the language. The closest it comes is the for (...in...) syntax that you've already rejected.
Look to your javascript library for this functionality. For example, the ever ubiquitous jQuery's each().

Alana Storm
- 164,128
- 91
- 395
- 599
-
1
-
1True, but for (...in...) can introduce some weird scoping bugs to your code, and then there's the whole hasOwnProperty weirdness. Plenty of reasons to avoid it even if you understand it. – Alana Storm Sep 24 '10 at 16:49
-
@AlanStorm Just came across a post explaining your concerns quite well, I understand it now! http://stackoverflow.com/questions/1885317/strange-behavior-in-javascript-enhanced-for-in-loop/1885365#1885365 – lsl Apr 16 '12 at 22:23