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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:53:01+00:00 2026-06-15T07:53:01+00:00

I am filtering through an array of objects and for each iteration in the

  • 0

I am filtering through an array of objects and for each iteration in the array I am taking a template and replacing it with the unique information (properties) from the object that we are currently on. Then the template is appended to the the DOM.

Please look at the first code block and notice that the line of code $('body').append(temp); is inside of the $.each iterator function

I am specifically interested in understanding –

  1. Is it because I have left $('body').append(temp); inside of the $.each iterator function that the DOM will be appended to every iteration?

  2. What if this is there is a list of 50 items in the array? In that case for every iteration are we appending something to the DOM?

Code

(function() {

        var content = [
            {
                title: "Speak of the devil and he shall appear",   
                thumbnail: 'images/bane.jpg',
            },
            {
                title: 'Me the joker',   
                thumbnail: 'images/Joker.jpg',
            },
        ];

             var template = $.trim( $('#blogTemplate').html() );

        $.each( content, function( index, obj ) { $(content).each(function() {})
            var temp =  
                    template.replace( /{{title}}/ig, obj.title )
                            .replace( /{{thumbnail}}/ig, obj.thumbnail );
            console.log(temp);

            $('body').append(temp);
        });

    })();

In order to solve this problem I believe instead it would be better to build up a string then throw that string into the DOM exactly once. So I have created a new variable called frag and set it equal to an empty string. Then we will filter through the array of objects, and for each object in the array rather than appending to the DOM we are going to say frag+= followed by the stacked .replace methods.

var template = $.trim( $('#blogTemplate').html() );
        var frag = '';

    $.each( content, function( index, obj ) { 
        frag +=     
                template.replace( /{{title}}/ig, obj.title )
                        .replace( /{{thumbnail}}/ig, obj.thumbnail );
        console.log(frag);
    });

    $('body').append(frag);

})();

And when we are finished we can say $('body').append(frag); outside of the $.each iterator function.

And this leads me on to my final question:

3a. I have moved $('body').append(temp); out of the $.each function, by doing this have I prevented the DOM being updated on every iteration of the $.each function?

  • 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-15T07:53:03+00:00Added an answer on June 15, 2026 at 7:53 am

    the answer to your questions

    1. yes, if you make an append inside an each block, the dom wil be appended on every iteration
    2. yes, for each of the 50 items on the array, you’ll be manipulating the DOM
    3. yes, by taking the append outside the each block, you are manipulating the DOM only once.

    EDIT: also, if you want to leave the append inside the each block, you should first remove the container element from the DOM, manipulate it, and the append it back to the DOM

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

Sidebar

Related Questions

I have an array of stdClass objects that are very simple, every object looks
Wrote a simple program that calls ls, then passes each line through regexp filtering
I'm experimenting with filtering through elements in parallel. For each element, I need to
I'm having a trouble filtering/ joining in-memory object lists with Linq to objects, and
I'm working my way through input filtering (files) and I just discovered that if
Basically I'm filtering through a BindingSource by input from a TextBox . I'm doing
I have an array of instances of my object - MyObject . Each instance
What's the cleanest way of filtering a Python string through an external program? In
Filtering QuerySets in Django work like the following: Entry.objects.filter(year=2006) How can I use filter
When filtering a queryset, I'm wondering if the following are equivalent. User.objects.filter(username='josh').filter(email__startswith='josh') User.objects.filter(username='josh', email__startswith='josh')

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.