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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:13:11+00:00 2026-06-18T16:13:11+00:00

I’m currently using a pseudo object structure in my javascript. For example: MyObj =

  • 0

I’m currently using a pseudo object structure in my javascript. For example:

MyObj = function(options) {
    options = options || {};

    this.valueA = options.valueA || 'A';
    this.valueB = options.valueB || 'B';
    this.valueC = options.valueC || 'C';
    this.valueD = options.valueD || 'D';
}

MyObj.prototype.doStuff() {
    // do stuff
}

Now I need to update an instance of the above, based on JSON data that only contains the attributes that have changed.

Currently I’m using a function kinda like:

MyObj.prototype.update(obj) {
    if(obj.valueA) this.valueA = obj.valueA;
    if(obj.valueA) this.valueB = obj.valueB;
    if(obj.valueA) this.valueC = obj.valueC;
    if(obj.valueA) this.valueD = obj.valueD;
}

Now in the above example it all all works fine, however some of my defined objects are quite large and some of the attributes are not simple (in that a if(x) is not adequate). This makes the code both messy and complex. The complexity IMHO is that I’m forced to maintain two separate places when I introduce new attributes. This concerns me as I’m aware how this can be a cause of introduced defects.

So my question is thus, is there a javascript mechanism that I can use to apply the attributes of an object to my instance of ‘MyObj’? I’m not happy with my approach and I’m finding it difficult to search for questions on this subject (suspect this is due to my lack of knowledge on the correct javascript terms for these entities).

Btw, the obj is created from parsing JSON data.

  • 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-18T16:13:12+00:00Added an answer on June 18, 2026 at 4:13 pm

    Are you looking for some sort of merge mechanism?

    Could this work for you?

    var recursiveObjectMerge = function( primary, overwrite ){
        var p;
        for( p in overwrite ){
            try{
                //try an update
                if( overwrite[p].constructor == Object ){
                    primary[p] = recursiveObjectMerge( primary[p], overwrite[p] );
                }
                else{
                    primary[p] = overwrite[p];
                }
            }
            catch( e ){
                // destination doesn't have that property, create and set it
                primary[p] = overwrite[p];
            }
        }
    
        // primary is modified (it's a reference), but pass it back
        // to keep up the idea that this function returns a result
        return primary;
    };
    
    • 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’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I am reading a book about Javascript and jQuery and using one of the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
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 am using JSon response to parse title,date content and thumbnail images and place
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small

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.