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

  • Home
  • SEARCH
  • 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 8810169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:03:44+00:00 2026-06-14T03:03:44+00:00

I’m thinking this must be a common problem but can’t seem to find the

  • 0

I’m thinking this must be a common problem but can’t seem to find the solution.
Using JSON config files to extend a jQuery object that contains objects and arrays.

For the objects and simple properties, I want to overwrite (as extend does nicely).

For the arrays there may or may not be existing items.

Currently an array just overwrites the first elements

var sourceObj = {propterty:"change Me",anArray:[{name:"first"},{name:"second"}]},
    configJSON = '{"propterty":"New Val","anArray":[{"name":"third"}]}',
    configObj = JSON.parse(configJSON);

$.extend(true,sourceObj,configObj);

http://jsfiddle.net/PmuwV/

This returns:

{propterty:"New Val" , anArray:[{name:"third"},{name:"second"}}

Can I instead get:

{propterty:"New Val",anArray:[{name:"first"},{name:"second"},{name:"third"}]}

while ALSO allowing for updating “first” and “second” objects?

"anArray":[{"name":"second","newProp":"add newProp to second"}]

Could/should extend be modified to compare array items and extend or add based on some rule or set property value such as “name”?

Thanks for any advice or pointers.

  • 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-14T03:03:45+00:00Added an answer on June 14, 2026 at 3:03 am

    I used this solution http://jsfiddle.net/PmuwV/2/
    modified from How can I merge properties of two JavaScript objects dynamically? also from JavaScript equivalent of jQuery's extend method

    requires isDOMNode()
    I just added in a jquery merge (yes I feel dirty too) on arrays in which duplicates will need to be cleaned up post merge.
    The Jquery source for extend does something very similar but i found this to be more readable.

    function mergeRecursive() {
      // _mergeRecursive does the actual job with two arguments.
      var _mergeRecursive = function (dst, src) {
        if ( isDOMNode(src) || typeof src!=='object' || src===null) {
          return dst; 
        }
    
        for ( var p in src ) {
    
    //my added bit here - [SB]
          if ($.isArray(src[p])){
              $.merge(dst[p],src[p]);
              var dupes = {},
                   singles = [];
              $.each(  dst[p], function(i, el) {
                 if ((dupes[el.name] > -1) &&  (el.name)) {
                     $.extend(singles[dupes[el.name]],el);
                 }else{
                      if (el.name ){
                         dupes[el.name] = i;
                      }
                     singles.push(el);
                 }
             });
             dst[p] = singles;
             }
             continue;        
          }
    //the rest is original - [SB]
    
          if( !src.hasOwnProperty(p) ) continue;
          if ( src[p]===undefined ) continue;
          if ( typeof src[p]!=='object' || src[p]===null) {
            dst[p] = src[p];
          } else if ( typeof dst[p]!=='object' || dst[p]===null ) {
            dst[p] = _mergeRecursive(src[p].constructor===Array ? [] : {}, src[p]); 
          } else {              
            _mergeRecursive(dst[p], src[p]);
          }
        }
        return dst;
      }
    
      // Loop through arguments and merge them into the first argument. 
      var out = arguments[0];
      if ( typeof out!=='object' || out===null) return out;
      for ( var i=1, il=arguments.length; i<il; i++ ) {
        _mergeRecursive(out, arguments[i]);
      }
      return out;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

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.