Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8461355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:51:26+00:00 2026-06-10T13:51:26+00:00

The following code refers to Firefox-specific JavaScript 1.7 implementation with Array comprehensions and iterators/generators.

  • 0

The following code refers to Firefox-specific JavaScript 1.7 implementation with Array comprehensions and iterators/generators. Note that as of this post’s writing, ‘Iterator’, ‘yield’, and array comprehensions are not yet supported in Webkit browsers (Chrome, Safari) and seem to only work on Firefox’s JavaScript engine (Google, liberateme).

NB: ECMAScript Harmony may bring about new changes to syntax and may make this post obsolete

var myobject = {'foo':5,'bar':10};
var myarray = [10,20,30];

for (var i in it) {
console.log(i);
}

Code:

JavaScript:

//if var it = Iterator(myobject);
['foo',5]
['bar',10]
//if var it = Iterator(myarray);
[0,10]
[1,20]
[2,30]

It seems like JS 1.7 Iterators ALWAYS return a ‘key’ and a ‘value’ (whether the key is an actual object key or the index of an array. Contrast this to Python, where Iterators are smart enough to return only 1 value if it is an array.

Python:

#if it = iter(myobject);
[foo,5]
[bar,10]
#if it = iter(myarray);
10,
20,
30

So the question is, how come Iterator() and iter() don’t do the same thing when I am trying to port Python-to-JavaScript functions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:51:28+00:00Added an answer on June 10, 2026 at 1:51 pm

    Answer:

    For the sake of porting code from Python Functions/Libraries to JavaScript, it is a bit frustrating to presume that Iterator() and iter() do the same thing, only to realize that (they are slightly different and results in the developer having to sometimes roll their own Iterator/Generator functions (using ‘yield’)*.

    for example, let’s say we wanted to roll the python’s items() function that is native to dict objects.

    var items = items = function(object) {
            var list = [];
            for (var i in object) {
                list.push([i,object[i]]);
            }
            return list;
        };
    

    items() works just fine when passing it the myobject JavaScript Object (granted, I am using nested arrays rather than tuples).

    items(myobject);
    //[['foo':5],['bar':10]]
    

    However, when constructing an iterator for items(myobject), we are actually iterating over a list so we want to make a custom generator that only yields the 2nd index to reproduce Python’s functionality:

        var arrayIterator = function(array){ 
        for (var i in iter) {
                yield i[1];//iterate over the non-index portion of the array iterable.
            }
        
        }
    
    arrayIterator(items(myobject))
    
    ['foo',5]
    ['bar',10]
    

    Notice that there is actually a bit of redundancy here. Notice that

    Iterator(myobject) = arrayIterator(items(myobject))
    

    So in the end, just be careful of what you are iterating over and see if you can speed up your call by avoiding calling functions that end up being each others’ inverse.

    I hope that all this is factually accurate – please correct me if I am wrong.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following code produces a nested array as a result for keys containing three items:
Following code is generated by a for loop. <form action=saveresponse.php method=POST name=mainForm> <input class=cbox_yes
I came across the following code and was wondering what the IMG.active refers too.
Imagine a program with two threads. They are running the following code (CAS refers
I have some code that, for the purposes of this question, boils down to
I am using the following code to get innerHTML of $(this) : var html=$(this).html();
Following code takes like 2500 milliseconds on an i7-*3.4 GHz windows-7 64-bit computer to
Following code worked fine abstract class FunctionRunnable<V> implements Runnable { protected abstract V calculate();
Following code: <%= render 'shared/error_messages', f.object %> where f.object is instance of a class
Following code gets executed whenever I want to persist any entity. Things seems to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.