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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:32:10+00:00 2026-06-13T08:32:10+00:00

I am trying to merge each object in an array of objects (that are

  • 0

I am trying to merge each object in an array of objects (that are all object literals) with a "prototype" that is an also an object literal:

var objArray = [{
    first: 123,
    second: "asd",
    third: function () {
        alert("hello");
    },
    items: {
        color: "brown",
        number: 10
    }
}, {
    first: 765,
    second: "lkj",
    third: function () {
        alert("goodbye");
    },
    items: {
        color: "yellow",
        number: 999
    }
}];

var obj2 = {
    first: 143,
    items: {
        color: "blue",
        number: false
    }
};

var combinedObjArray = [];


var i;
for (i = 0; i < objArray.length; i += 1) {
    var newObj = $.extend(true, obj2, objArray[i]);
    combinedObjArray.push(newObj);
}

alert(JSON.stringify(combinedObjArray));​

My issue is that "newObj" just creates a pointer, instead of creating unique objects (I think):

http://jsfiddle.net/RbabL/

  • 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-13T08:32:11+00:00Added an answer on June 13, 2026 at 8:32 am

    This line…

    var newObj = $.extend(true, obj2, objArray[i]);
    

    … should actually be written as …

    var newObj = $.extend(true, {}, obj2, objArray[i]);
    

    … otherwise you’ll augment an obj2 each time it’s called:

    Keep in mind that the target object (first argument) [or second argument, when deep param is used] will be modified,
    and will also be returned from $.extend(). If, however, we want to
    preserve both of the original objects, we can do so by passing an
    empty object as the target:

    var object = $.extend(true, {}, object1, object2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to merge cell-array of strings delimiting each by new line to one
I'm trying to merge a lot of pdf's and for each pdf I want
I'm trying to merge logs from several servers. Each log is a list of
I am trying to merge some code behind code for .net that I found
I'm trying to merge two arrays for the values of array1 that are set
I'm trying to merge these 2 arrays $arr1 = array('a' => 1, 'b' =>
I am trying to merge multiple excel files using DataTable.Merge() option For Each fileName
I've got a permissions/group based setup that I'm trying to merge results on. For
I am trying to figure out how to merge two complex object instances using
I'm trying to merge these 3 functions together so I can get all my

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.