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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:19:08+00:00 2026-05-27T07:19:08+00:00

I have a loop goes through an array of objects MyArrayOfObjects and then pushes

  • 0

I have a loop goes through an array of objects MyArrayOfObjects and then pushes the objects to a new array like this:

var NewArray = new Array();

for (i = 0; i < MyArrayOfObjects.length; i++) {

  TempObject = null;
  TempObject = new Object();

 // I have logic that copies certain properties but not others
 // but overall it looks like this:

  TempObject.prop1 = MyArrayOfObjects[i].prop1;
  TempObject.prop2 = MyArrayOfObjects[i].prop2;

  NewArray.push(TempObject);
}

As I loop through MyArrayOfObjects, I clear the TempObject and create a new one each time. Does NewArray contain the objects that I’m copying or just a reference to the objects copied and that then become deleted as the loop iterates?

Thanks.

  • 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-05-27T07:19:09+00:00Added an answer on May 27, 2026 at 7:19 am
    > var NewArray = new Array();
    

    It is generally considered better to use an array literal to create an array. Variable names starting with a capital letter are, but convention, used for constructors. Using “new” at the start of a variable name can easily slip to become “new Array”, and the name should reflect its purpose, so something like the following might be better:

    var objectArray = [];
    

    .

    > for (i = 0; i < MyArrayOfObjects.length; i++) {
    

    You should always declare variables, especially counters as undeclared variables are made properties of the global object (effectively global variables) when they are first assigned a value. Also, it is considered better to store the length of the array than get it in each iteration:

    for (var i = 0, iLen = MyArrayOfObjects.length; i < iLen; i++) {
    

    .

    >   TempObject = null;
    >   TempObject = new Object();
    

    Again, declare variables. Assigning a value of null serves no useful purpose when you’re going to assign some other value immediately afterward. Just do the second assignment (and use a literal):

      var TempObject = {};
    

    .

    >  // I have logic that copies certain properties but not others
    >  // but overall it looks like this:
    > 
    >   TempObject.prop1 = MyArrayOfObjects[i].prop1;
    >   TempObject.prop2 = MyArrayOfObjects[i].prop2;
    > 
    >   NewArray.push(TempObject);
    

    At this point, TempObject and NewArray[NewArray.length – 1] both reference the same object.

    > }
    

    As I loop through MyArrayOfObjects, I clear the TempObject and create
    a new one each time.

    There is no need to “clear” the object, just assign a new value to the variable. In javascript, all variables have a value that might be a primitive (e.g. string, number) or a reference to an object (e.g. Object, Array, Number, String)

    Does NewArray contain the objects that I’m
    copying or just a reference to the objects copied and that then become
    deleted as the loop iterates?

    It contains references to the new objects created on each iteration.

    As variables hold references to objects, assigning a new value to the variable doesn’t do anything to the object. When an object is no longer referenced by any variable or object property, it is made available for garbage collection and may be removed automatically at some later time when garbage collection runs.

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

Sidebar

Related Questions

I have a loop that goes through all the news items we have on
I have a loop like this: for i=1:no %some calculations fid = fopen('c:\\out.txt','wt'); %write
I have a loop that runs through a variety of websites and I'd like
I have a loop which goes through a video with some processing/calculations of the
I have a model Campaign and the campaign/show goes through a loop of the
I have a while loop that goes through a table and I echo the
I've got a loop which goes through various products being imported. Within this loop
In PHP, I have an associative array like this $a = array('who' => 'one',
I have a while loop in Main() which goes through several methods. Although one
I have a loop, that goes through some entries. In the loop I'm getting

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.