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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:15:58+00:00 2026-05-23T11:15:58+00:00

here’s the code var arr = [1,2,3]; var entry = {}; var test =

  • 0

here’s the code

var arr = [1,2,3];
var entry = {};
var test = [];
for(var i=0;i<arr.length;i++){
   entry.id = arr[i];
   test.push(entry);
}

console.log(test);

i wanna output a array with three different object. but now my objects are all the same, why?

[Object { id=3}, Object { id=3}, Object { id=3}]

  • 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-23T11:15:59+00:00Added an answer on May 23, 2026 at 11:15 am

    The problem is that you’re reusing the same object on all of the loop iterations. Create a new entry object within the loop:

    var arr = [1,2,3];
    var entry;                      // (modified, don't create here)
    var test = [];
    for(var i=0;i<arr.length;i++){
       entry = {id: arr[i]};        // (modified, create new one here, with `id` prop)
       test.push(entry);
    }
    
    console.log(test);
    

    Otherwise, if you just assign to entry.id on each loop, you’re just changing the id property on the same object. When you push an object reference onto an array, you’re just pushing a reference, not copying the object.

    You can, of course, do away with the entry variable entirely if you like, but you may want to keep it for clarity, etc. Here’s without:

    var arr = [1,2,3];
    var test = [];
    for(var i=0;i<arr.length;i++){
       test.push({id: arr[i]});
    }
    
    console.log(test);
    

    Somewhat off-topic:

    That structure creating an object and simultaneously assigning properties to it is called an object literal. If you had multiple properties, you’d separate their initializers with commas. E.g.:

    var obj = {a: 1, b: 2, c: 3};
    

    is the same as

    var obj = {};
    obj.a = 1;
    obj.b = 2;
    obj.c = 3;
    

    The values (the right-hand side of the :) can be expressions just as in an assignment statement. The names (the left-hand side of the :) are either literals, as above, or strings:

    var obj = {"a": 1, 'b': 2, c: 3};
    //         ^^^     ^^^     ^---- literal is fine
    //          |       +----------- string (with single quotes) also fine
    //          +------------------- string (with double quotes) also fine
    

    Off-topic side note: If at some point you find yourself using JSON for data exchange, the rules are similar but slightly more restrictive (JSON is a subset of object literal notation). Specifically, property names must be in double quotes, and all string values (even on the right-hand side) must also be in double quotes.

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

Sidebar

Related Questions

Here my code: $(document).ready(function() { $('#mid_select').live('click', function(e){ $('#middle').load( $(this).attr('href') + ' #middle'); var page
Here is the test code that I am using : public class IOConnectDirect extends
Here is my code, which takes two version identifiers in the form 1, 5,
Here is the code I'm using http://jsbin.com/evike5/edit When the jQuery UI dialog is fired
Here's some of my production code (I had to force line breaks): task =
Here's my search array: $aExt = array('png','jpg','gif'); I want to search through the variable:
Here is a great article about GC may occur at unexpected point of code
Here is my javascript code for a cursor focus function to go to username
Here is an implementation of a thread safe queue. The push and pop won't
here are 2 screen shots when i try to debug my code in visual

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.