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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:25:52+00:00 2026-05-16T17:25:52+00:00

Are there any existing methods to append an object to another object? I’ve had

  • 0

Are there any existing methods to append an object to another object?

I’ve had a quick stab at throwing this together but I’m not sure about a couple of things:

  • Am I handling methods correctly? I added an exception for append but what about when other prototype functions exist? Should I just ignore functions in the new class?

  • What should I do about null / undefined values?

  • Also, I’ve just thought about arrays.. what would be the best way to handle arrays? typeof reports as ‘object’.. I guess that testing the Array().constructor value would be the way forward

Other than these couple of issues it seems to function as I want it to (overwriting/adding individual parts of the existing object only where it exists in the new object). Are there any edge cases I’ve missed?

Object.prototype.append = function(_newObj)
{
  if('object' !== typeof _newObj) {
    console.info("ERROR!\nObject.prototype.append = function(_newObj)\n\n_newObj is not an Object!");
  }

  for (newVar in _newObj)
  {
    switch(typeof _newObj[newVar]){
      case "string":
        //Fall-through
      case "boolean":
        //Fall-through
      case "number":
        this[newVar] = _newObj[newVar];
      break;

      case "object":
        this[newVar] = this[newVar] || {};
        this[newVar].append(_newObj[newVar]);
      break;

      case "function":
        if(newVar !== 'append'){
          this[newVar] = _newObj[newVar];
        }
      break;
    }
  }

  return this;

}


var foo = { 1:'a', 2:'b', 3:'c' };
var bar = { z: 26, y: 25, x: 24, w: { 'foo':'bar'}, v: function(){ alert('Hello world"'); } };

foo.append(bar);
console.info(foo);
  • 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-16T17:25:52+00:00Added an answer on May 16, 2026 at 5:25 pm

    I like it. I’ve used a similar, but not as robust method in my code. But it would probably be safer to implement it as a static method for the Object class:

    if (typeof Object.merge !== 'function') {
        Object.merge = function(_obj, _newObj)
        {
            if("object" !== typeof _obj)
                console.info("ERROR!\nObject.merge = function(_obj, _newObj)\n\n_obj is not an Object!");
            if("object" !== typeof _newObj)
                console.info("ERROR!\nObject.merge = function(_obj, _newObj)\n\n_newObj is not an Object!");
    
            for (newVar in _newObj)
            {
                switch(typeof _newObj[newVar]){
                    case "object":
                        _obj[newVar] = _obj[newVar] || {};
                        Object.merge(_obj[newVar], _newObj[newVar]);
                        break;
                    case "undefined": break;
                    default: // This takes care of "string", "number", etc.
                        _obj[newVar] = _newObj[newVar];
                        break;
                }
            }
            return _obj;
        }
    }
    
    var foo = { 1:'a', 2:'b', 3:'c' };
    var bar = { z: 26, y: 25, x: 24, w: { 'foo':'bar'}, v: function(){ alert('Hello world"'); } };
    Object.merge(foo, bar);
    console.info(foo);
    

    To answer your questions, I haven’t found any better methods (outside of a framework) to do this either. For null/undefined values, you if the _newObj has null/undefined values, then shouldn’t your recipient object also have those (i.e. don’t make any special case for those)?

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

Sidebar

Related Questions

Is there any way that we can mock certain methods using existing objects ?
Is there any existing solution to embed the app.config file into the resulting assembly?
Are there any existing add-ons which would provide the functionality Upload image from local
Are there any existing libraries in existence that will parse a datetime from a
Are there any existing algorithms for finding and avoiding problematic areas ( swamps ,
I want my Nancy site to accept Google/Facebook/OpenID authentication. Are there any existing solutions
In Guava, if I have a Collection<T> - is there any existing function in
Are there any pre-existing solutions out there which would extend the built in SQL
I want to know if there are any existing benchmarks and sizing information for
Are there any ways to make javac use an existing OSGi-environment for the resolution

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.