variable scope in for loops

58 views
Skip to first unread message

Marc Phillips

unread,
May 23, 2015, 8:23:06 PM5/23/15
to nod...@googlegroups.com

I'm building up an object (or trying to) iterating though a json object and gathering info based various rest calls:
<pre>
        var generate = {};
        for(var protocol in buildout.protocols){
                for(var item in buildout.protocols[protocol]){
                  switch (item) {
                   .....
</pre>

The problem I'm having is that after the for loop, the generate array only holds the value of the last update, all the other info is missing.
I'm sure I'm doing something obviously wrong here (converting some automation scripts from perl to node).

John Shaver

unread,
May 23, 2015, 8:57:18 PM5/23/15
to nod...@googlegroups.com

Hey Marc,

Generate is not an array, but an object  (which can be used as a key/value store).  There are a couple of possible problems, assigning to the object instead of a property on the object, or assigning everything with the same key.

Unfortunately, without more code (like the code that assigns the data to the generate object) it's hard to tell what is happening.

-John

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/4c4c2ab2-2077-4ff2-a29f-a9f826a3e210%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Prabhash Rathore

unread,
May 23, 2015, 8:57:26 PM5/23/15
to nod...@googlegroups.com
Hi Marc, Looks like you are overwriting the objects with the next subsequent values and hence you are left with the last value in your object. Make sure you copy values in new object for each iteration instead of just overwriting the same object.

--

Ryan Schmidt

unread,
May 23, 2015, 8:57:38 PM5/23/15
to nod...@googlegroups.com
Show us enough of your code that we can run it on our own systems. You haven't shown any code that sets anything in generate, for example, nor have you shown a definition of buildout. Also, you referred to generate as an array, but have declared it as an object; make sure you're using the right data type.


Peter Rust

unread,
May 24, 2015, 9:48:35 AM5/24/15
to nod...@googlegroups.com
As others have said, we need more code to know for sure, but given the subject line, "variable scope in for loops" and the fact that you're using node, there's a decent chance that you're running into the gotcha described in the MDN article on Closures: Creating closures in loops: A common mistake. The idea being that if you have an asynchronous call inside your switch that takes a function, that function will be run later, after both of the outer for loops are done iterating, so both "protocol" and "item" will always be on the last item.

Javascript scopes variables at the function level, so even though you can declare variables inside for statements, javascript "hoists" the variable declaration to the top of the function it is contained in.
Reply all
Reply to author
Forward
0 new messages