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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:50:29+00:00 2026-06-18T08:50:29+00:00

I have an array of object that I’m trying to iterate over and build

  • 0

I have an array of object that I’m trying to iterate over and build markup for a list from. I’m using an array to store the deferred requests and $.when to inject the concatenated markup into the DOM after all the request have been completed.

Here’s the code:

function populateItemList(items) {
  var output = '',
    item = null,
    template = null,
    defers = [];

  for (var i = 0, j = items.length; i < j; i++) {
    item = items[i];

    defers.push(function() {
      $.get('/item/' + item.id + '/template')
      .done(function(source) {
        template = Handlebars.compile(source);
        output += template(item);

        console.log(item.id + ': done');
      })
    });
  }

  $.when.apply(window, defers).done(function() {
    $('.itemListContainer').html(output);
    $('.itemList a').click(onItemLinkClick);

    console.log('when: ' + output);
  });
}

However, I’m not having any success. I’m running into one of two problems:

  1. If I do wrap the deferred items in a function (as in the code above), the requests do not get executed, but the $.when does. But since the requests didn’t get executed, the output is empty.
  2. If I don’t wrap the deferred items in a function, the requests get executed (the correct number of times, but the item.id logged to the console is always the id of the last item), and the $.when does not get executed.

I’m aware that this is very similar to this question, and I’m using that code as a basis for mine, but I’m still having these problems. Any ideas?

  • 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-18T08:50:29+00:00Added an answer on June 18, 2026 at 8:50 am

    When you wrap the code in a function like that, the code never gets executed. What you push into the array is a reference to the function, not the result of the AJAX call.

    When you don’t wrap the code in a function it does get executed, but as you are using the local variable item in the callback, you will be using the value that the variable has at the time that the responses arrive, not when the request was sent. As the requests are asynchronous, and because Javascript is single threaded, no callbacks will be called until you have returned from the populateItemList function, so the item variable will always be the last item.

    You should wrap the code in a function, that will let you create an item variable for each iteration, but you should also execute the function immediately, and push the return value into the array:

    for (var i = 0, j = items.length; i < j; i++) {
    
      var result = (function(item) {
        return $.get('/item/' + item.id + '/template')
        .done(function(source) {
          template = Handlebars.compile(source);
          output += template(item);
          console.log(item.id + ': done');
        });
      }(items[i]));
    
      defers.push(result);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I was trying to remove objects from object array that I have and
I have been trying to assign value to an object array that is defined
I have an ATL COM object that I am using from C#. The interface
So I have an array that's being pulled from a json object that I'd
I have an array object that is an output of a magento fetchall from
I have an object that contains a array. On initialization of this object, the
I have an array that looks like this: [ Object actions: Array[2] comments: Object
I have a json object that looks something like this: Array ( [algorithm] =>
I have a custom object that I was adding to an array via a
I have an array that contains @sign in the object. I can't access that

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.