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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:19:08+00:00 2026-06-05T19:19:08+00:00

I have two javascript objects: var a = { x: 1, y: { faz:

  • 0

I have two javascript objects:

var a = {
    x: 1, 
    y: {
        faz: 'hello', 
        baz: ''
    },
    z: [1, 2]
};


var defaults = {
    x: 2,
    y: {
        faz: '', 
        baz: ''
    },
    z: [1, 2]
};

I want to only keep the fields of a that are different from the default:

a = remove_defaults(a, defaults); // <---- i need this fnc
{
    x: 1,
    y: {
        faz: 'hello'
    }
}

The goal is to remove default values from an object that serves as a state (via URL). The state can have nested fields, so a shallow compare is not enough. The leaf values are all primitive (number, string, bool).

(this is a bit like the opposite of underscore.js‘s _.defaults() method)

What is the best way to achieve this?


The solution can use underscore.js if that helps, but no jquery.

  • 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-05T19:19:09+00:00Added an answer on June 5, 2026 at 7:19 pm

    My own take:

    function no_defaults(obj, defaults) {
        if ((obj instanceof Array) && (defaults instanceof Array)) {
            var result = _.difference(obj, defaults);
            return _.isEmpty(result) ? undefined : result;
        }
        if ((obj instanceof Array) || (defaults instanceof Array))
            return _.clone(obj);
        if (typeof obj == "object" && typeof defaults == "object") {
            var result = {};
            for (var prop in obj) {
                var res = prop in defaults ? no_defaults(obj[prop], defaults[prop]) : _.clone(obj[prop]);
                if (res !== undefined)
                    result[prop] = res;
            }
            return _.isEmpty(result) ? undefined : result;
        }
        return _.isEqual(obj, defaults) ? undefined : _.clone(obj);
    }
    

    Note this deep-processes objects, but not arrays. Arrays are only processed for shallow difference of their direct elements — if the elements themselves need no_defaulting, this function won’t do. But this is fine for my intended use case.

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

Sidebar

Related Questions

I have two Javascript objects similar to so.... var Object2 = new (function() {
I have two or more javascript objects. I want to merge them adding values
I have two JavaScript arrays: var array1 = [Vijendra,Singh]; var array2 = [Singh, Shakya];
I have two pieces of javascript that I would like to work together. I
I have two simple textareas where in i want to highlight the javascript code
I have two JSON objects in Javascript, identical except for the numerical values. It
I have the following code that creates two objects (ProfileManager and EmployerManager) where the
I have created a javascript object var spanglist = { one: q1, two:q2, three:q3,
I have two Javascript files, that I will significantly streamline to simplify the issue
I have a javascript object with two array's as shown, var Object = {'name':

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.