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 7910371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:50:45+00:00 2026-06-03T12:50:45+00:00

I have an object with arrays as values. people = { ‘steve’:[‘foo’,’bar’], ‘joe’:[‘baz’,’boo’] }

  • 0

I have an object with arrays as values.

people = {
    'steve':['foo','bar'],
    'joe':['baz','boo']
}

For each key, I would like to loop over the values in the corresponding array. Simple enough:

for ( var person in people ) {
    person.forEach( function(item) {
      console.log(item)
    })
}

But JSHint complains:

Don't make functions within a loop.

Is this truly an issue with my code? I quite like the short ES5 for loop syntax. Do I need to use the ES3 style or change my code in some other way?

  • 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-03T12:50:46+00:00Added an answer on June 3, 2026 at 12:50 pm

    There are two issues there, the one that JSHint is warning you about, and a more fundamental one.

    The thing that JSHint is warning you about is that in theory, every time that loop runs, a new function is created. This would be better:

    for ( var person in people ) {
        person.forEach(handlePerson);
    }
    function handlePerson(item) {
      console.log(item)
    }
    

    I say “in theory” because although the spec requires that a new function object be created each time, that doesn’t mean engines can’t reuse the underlying implementation of the function, and it doesn’t mean that engines can’t reuse the same function object if you didn’t assign any other properties to it or keep a reference to it. I asked the V8 guys about it (V8 being the JavaScript engine in Chrome), and they said that Chrome will “…in most cases…” reuse the underlying function implementation for different function objects created at the same point in the source code, and that they would “expect” that most other engines would do the same.

    So JSHint may be being a bit over-the-top in this particular case. But it’s frequently a useful warning, particularly if the functions you’re creating inside the loop refer to variables whose contents change during the loop, which is the classic closure error people make.

    But more fundamentally, person is a String (it’s the name of a property in people), and String doesn’t have forEach. You wanted:

    for ( var person in people ) {
        people[person].forEach(handlePerson);
    }
    function handlePerson(item) {
      console.log(item)
    }
    

    …e.g., people[person] to get the array for that key.

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

Sidebar

Related Questions

In my model I have three complex scopes returning the Discount object arrays. Each
I have a List with array of object values. I have to display value
I have a list of Object arrays (List) That I am going to pass
I have the following issue: A java object contains two arrays of core datastore
The only examples I have been able to find of people using $.each are
I have an object which I must validate values off the problem, some of
I have been trying to assign value to an object array that is defined
I have an object whose value may be one of several array types like
I have these code in my views.py: array = [] p = Person.objects.filter(client=1,status='Complete').values('name', 'age',
I have a bidimensional Object array in Java. Some indices aren´t used, because they

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.