3

I have two JSON objects that I want to merge in such a way that only the values of the properties in the first object that already exist are updated.

I want to extend the first object with the second

var obj1 = {a:'apple', b:'banana', c:'cucumber'};
var obj2 = {a:'aarbei', b:'beet', d:'durian'};

and get this result

{a:'aarbei', b:'beet', c:'cucumber'};

instead of (which I get using $.extend)

{a:'aarbei', b:'beet', c:'cucumber', d:'durian'};

In reality my JSON objects can be much bigger and contain nested objects/arrays of unknown complexity

3
  • 4
    why did you chose that username?! i spit my whole coffee at the wall! Commented Feb 12, 2014 at 11:02
  • 2
    to save people from the possible risks of consuming hot beverages Commented Feb 12, 2014 at 11:03
  • I'd say that's not extending but a simple overwrite of obj1 with obj2 Commented Feb 12, 2014 at 11:06

4 Answers 4

4

What about simple for loop:

for (var key in obj2) {
    if (key in obj1) {
        obj1[key] = obj2[key];
    }
}

For nested object you can wrap this code into recursive function:

function mergeObj(a, b) {
    for (var key in b) {
        if (key in a) {
            a[key] = typeof a[key] === 'object' && typeof b[key] === 'object' ? mergeObj(a[key], b[key]) : b[key];
        }
    }
    return a;
}

Demo: http://jsfiddle.net/rHUM6/

Sign up to request clarification or add additional context in comments.

4 Comments

This ignores the problem of nested objects/arrays
Absolutely, you should have stated it in question!
I do. Read the last line
Oh, no problem - we need recursion.
1

I took a different approach, trimming the unwanted properties from a copy of each object and then using those objects to extend a new object.

var obj1 = {a:'apple', b:'banana', c:'cucumber'};
var obj2 = {a:'aarbei', b:'beet', d:'durian'};
var keepProps = ["a", "b", "c"];

var trimmedObj1 = trim(obj1, keepProps);
var trimmedObj2 = trim(obj2, keepProps);

var obj3 = $.extend({}, trimmedObj1, trimmedObj2);
console.log(obj3);

function trim(obj, properties){
    var copy = $.extend({}, obj);
    for(x in obj){
        if(obj.hasOwnProperty(x) && properties.indexOf(x) == -1){
            delete copy[x];
        }
    }
    return copy;
}

JS Fiddle: http://jsfiddle.net/m8NCc/

1 Comment

I like this but also doesnt take into account the nested objects/arrays
1

This should work...

function mergeObjs () {
    for (var key in obj2) {
         if (obj1.hasOwnProperty(key)) {
             obj1[key] = obj2[key]
         }
    }
    return obj1;
}

var mergedObjs = mergeObjs();

1 Comment

This ignores the problem of nested objects/arrays
0

Javascript inherintance is not so complex.

If you have an object, let's say ObjectA like

function ObjectA () {
    this.a = "Hello";
    this.a = "World";
    this.c = 30;
}

Then you want to inherit this object :

function ObjectB () {
    ObjectA.call(this);// Call the super constructor
}
ObjectB.prototype = Object.create(ObjectA.prototype); // Inheritance
ObjectB.prototype.constructor = ObjectB; 

Finally, you can acces and/or modify any property from ObjectA like :

var o = new ObjectB();
o.a = "Hi";

If you need to parse JSON object, see this (quoting myself :/).

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.