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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:22:37+00:00 2026-06-17T07:22:37+00:00

Lets take a look at the example below: var ref = { "fullName": {

  • 0

Lets take a look at the example below:

var ref = {
    "fullName": {
        "rules": {
            "type": "string",
            "minLength": 4,
            "maxLength": 64
        },
        "description": "Full name of a user."
    }
};

var user = {
    "fullName": {
        "rules": {
            "required": true,
            "maxLength": 128
        },
        "message": "You have submitted a wrong full name."
    }
};

Now what I want is this:

  1. Merge objects & properties.
  2. Keep the properties of the second object IF they are set already (maxLength)

Below is the result that I expect:

var res = {
    "fullName": {
        "rules": {
            "required": true,
            "maxLength": 128
            "type": "string",
            "minLength": 4
        },
        "description": "Full name of a user.",
        "message": "You have submitted a wrong full name."
    }
};

What I have tried:

function mergeNestedObjects(firstObject, secondObject) {
    var finalObject = {};
    
    for (var propertyKey in firstObject) {
        var propertyValue = firstObject[propertyKey];
        
        if (typeof(propertyValue) === "object") {
            finalObject[propertyKey] = mergeNestedObjects(firstObject[propertyKey], secondObject[propertyKey]);
        } else if (secondObject[propertyKey] === undefined) {
            finalObject[propertyKey] = firstObject[propertyKey];
        } else {
            finalObject[propertyKey] = secondObject[propertyKey];
        }
    }
    
    return finalObject;
}

The function above merges but somehow doesnt nest the properties.

UPDATE & ANSWER got it working, I forgot too itterate through the second object, how dumb. Thanks to @AnthonyGrist

function mergeProperties(propertyKey, firstObject, secondObject) {
    var propertyValue = firstObject[propertyKey];

    if (typeof(propertyValue) === "object") {
        return mergeNestedObjects(firstObject[propertyKey], secondObject[propertyKey]);
    } else if (secondObject === undefined || secondObject[propertyKey] === undefined) {
        return firstObject[propertyKey];
    }
    
    return secondObject[propertyKey];
}

function mergeNestedObjects(firstObject, secondObject) {
    var finalObject = {};
    
    // Merge first object and its properties.
    for (var propertyKey in firstObject) {
        finalObject[propertyKey] = mergeProperties(propertyKey, firstObject, secondObject);
    }

    // Merge second object and its properties.
    for (var propertyKey in secondObject) {
        finalObject[propertyKey] = mergeProperties(propertyKey, secondObject, firstObject);
    }
    
    return finalObject;
} 
  • 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-17T07:22:38+00:00Added an answer on June 17, 2026 at 7:22 am
    function mergeProperties(propertyKey, firstObject, secondObject) {
        var propertyValue = firstObject[propertyKey];
    
        if (typeof(propertyValue) === "object") {
            return mergeNestedObjects(firstObject[propertyKey], secondObject[propertyKey]);
        } else if (secondObject[propertyKey] === undefined) {
            return firstObject[propertyKey];
        }
    
        return secondObject[propertyKey];
    }
    
    function mergeNestedObjects(firstObject, secondObject) {
        var finalObject = {};
    
        // Merge first object and its properties.
        for (var propertyKey in firstObject) {
            finalObject[propertyKey] = mergeProperties(propertyKey, firstObject, secondObject);
        }
    
        // Merge second object and its properties.
        for (var propertyKey in secondObject) {
            finalObject[propertyKey] = mergeProperties(propertyKey, secondObject, firstObject);
        }
    
        return finalObject;
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So lets take a look onto a littel bit modified example code: #include <stdio.h>
Lets take a look at the following code: mongodb.js var mongo = require('mongodb') ,
Lets take stackoverflow as an example: Post Postid Title Mess -------------------------------------------------- 1 Title1 This
I see this quite a bit with online apps. Lets take, for example, the
Let's take a look at this example: class Author(models.Model): ... class Book(models.Model): author =
Take a look at the first example of http://code.google.com/intl/sv-SE/appengine/docs/java/datastore/jdo/relationships.html (Owned One-to-One Relationships). I dont
This would be useful for consistently naming methods, for example let's take a look
I'm posting this because i'm curious about something. For example, let's take a look
Take a look at the example here: http://docs.angularjs.org/api/ng.filter:filter You can search by any of
EDIT To simplify this further. Let's take a look at this structure: Table1: idProduct

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.