0

Quick question - assume we have these two objects:

var obj={str:'',int:0,bool:false,arr:[]};
var paramObj={str:'Hey',bool:true};

The .extend() function in jQuery works like this: (to my knowledge)

$.extend(obj,paramObj);
// obj={str:'Hey',int:0,bool:true,arr:[]};

But I want something that works like this, but in reverse, like so:

$.extendRev(paramObj,obj);
// paramObj={str:'Hey',int:0,bool:true,arr:[]};

Is there a function like this in the jQuery (or native) language, which merges two objects together, keeping object values in the first object, only adding object values from the second object if they dont already exist in the first.

Or am I going to have to code this out from scratch?

4
  • 2
    because then values in obj overwrite the values in paramObj, which is not what I want. Commented Jan 21, 2014 at 16:04
  • dont understand why I am getting downvotes? is this not a legitimate question? Commented Jan 21, 2014 at 16:06
  • So what output are you expecting here. I think you're getting downvoted as your question is very poorly explained. Commented Jan 21, 2014 at 16:08
  • I want paramObj to contain its original values, plus any values from obj that it is missing, without overwriting the values from paramObj. like so: paramObj={str:'Hey',int:0,bool:true,arr:[]}; Commented Jan 21, 2014 at 16:10

5 Answers 5

9

You can simply use an empty object as the starting point:

$.extend({},obj,paramObj);

Chrome's console shows that the result is

Object {str: "Hey", bool: true, int: 0, arr: Array[0]}
Sign up to request clarification or add additional context in comments.

Comments

2

Just to show you what the vanilla JS would look like, this will merge b into a:

function extendObject(a, b) {
  if (a && b) {
    for (var key in b) {
      if (!a.hasOwnProperty(key)) a[key] = b[key];
    }
  }
};

Demo

Comments

1

if you want to use plain javascript you could do something like this:

var a = {str:'Hey',bool:true};
var b = {str:'',int:0,bool:false,arr:[1,2,3]};

function x_extend(obj1, obj2){
    for(prop in obj2){
        if (obj1.hasOwnProperty(prop) === false){
            obj1[prop] = obj2[prop];
        }
    }
}

x_extend(a, b);

console.log(a);

Comments

0

extend function can merge multiple objects too..

var obj={str:'',int:0,bool:false,arr:[]};
var paramObj={str:'Hey',bool:true};
var p= {}
$.extend(p,obj, paramObj);

Comments

0

This arises often when you need to accumulate settings by traversing a hierarchy of settings providers from leaf to root. Here's how I do it:

while(provider) {

    $.extend(accumulator, $.extend({},provider.defaults,accumulator)); // <---
    provider = provider.parent;
}

The inner extend creates a separate, properly merged object. Then the outer extend copies the properties onto the accumulator.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.