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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:12:40+00:00 2026-06-13T01:12:40+00:00

The following javascript code gives me the Don’t make functions within a loop. error

  • 0

The following javascript code gives me the Don’t make functions within a loop. error

/* Get the favorite products from the data using the id */
productDataCategory[FAVORITES].length = 0/* empty favorites productDataCategory */;
for ( i = 0; i < currentUser.favorites.length; i++) {
    product = $.grep(productData, function(e){ return e.id === currentUser.favorites[i]; })[0];
    productDataCategory[FAVORITES].push(p);
}

I’ve looked the question up and saw some similar questions asked by other members
How to get around the jslint error ‘Don’t make functions within a loop.’
Don’t make functions within a loop

The problem I have is that I’m using a $.grep function inside the loop to find the product in an array.
I dont know how to resolve this issue with the answers in the above questions.


Data from a logged user

{
    "user": "MBO",
    "email": "my@mail.com",
    "username": "Marcel",
    "photoURL": "url_here",
    "favorites": [13,25,40,56]
}
  • 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-13T01:12:41+00:00Added an answer on June 13, 2026 at 1:12 am

    Put the function outside the loop:

    /* Get the favorite products from the data using the id */
    productDataCategory[FAVORITES].length = 0/* empty favorites productDataCategory */;
    var f = function(e){ return e.id === currentUser.favorites[i]; };
    for ( i = 0; i < currentUser.favorites.length; i++) {
      product = $.grep(productData, f)[0];
      productDataCategory[FAVORITES].push(p);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I run the following JavaScript code which gives my the list of parameters from
The following code in javascript gives me the error this.callback is not a function
The following javascript code gives me >success-<, i.e. empty data. Pasting the url in
Following javascript code gives me output like : 1314066350368 new Date().getTime(); I would like
MSDN gives the following Javascript code for querying the Bing Image Search API. It
The following Pseudo and JavaScript code is a extract from the implementation of a
The following javascript code seems to leak memory but I’m not sure why. I’ve
The following javascript code concatenates the matches found by a regular expression. The regular
I have the following Javascript code that works perfectly: $(document).ready(function() { $(#Select1).setDefault(); $(#Select2).setDefault(); $(#Select3).setDefault();
I have the following JavaScript code: var x = ['CFMG','JMFMG','CPMAF']; var y = $.param({'test':x});

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.