JavaScript merge two objects without adding new properties merge objects without overwriting,javascript merge objects without overwriting,merge

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

Answer:1

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/

Answer:2

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/

Answer:3

This should work...

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

var mergedObjs = mergeObjs();
Answer:4

I have a button right this: <button type="submit" class="btn blue pull-right" id="RequestNewPwd"> Submit <i class="m-icon-swapright m-icon-white"></i> <...

I have a button right this: <button type="submit" class="btn blue pull-right" id="RequestNewPwd"> Submit <i class="m-icon-swapright m-icon-white"></i> <...

  1. change jquery button text
  2. change jquery button color
  3. jquery change button text on click
  4. jquery change button value
  5. jquery change button background color
  6. jquery change button label
  7. jquery change button class
  8. jquery change button type
  9. jquery change button name
  10. jquery change button text onclick toggle
  11. jquery change button value onclick
  12. jquery change button onclick
  13. jquery change button title
  14. jquery change button disabled
  15. jquery change button
  16. jquery change button onclick function
  17. jquery change button id
  18. jquery change button color on click
  19. jquery change button text dynamically
  20. jquery change button type to submit

I would like to set attrs to undefined from unit test, I tried few approaches and didn't get to set it to undefined. Below is my directive: angular.module('myApp').directive('someElement', function ()...

I would like to set attrs to undefined from unit test, I tried few approaches and didn't get to set it to undefined. Below is my directive: angular.module('myApp').directive('someElement', function ()...

I am just beginning to learn AJAX and have a very newb question. I am reading the book "AJAX & PHP: Building Responsive Web Applications" by Packt Publishing, Darie et al. Ch. 2, Page 50 shows ...

I am just beginning to learn AJAX and have a very newb question. I am reading the book "AJAX & PHP: Building Responsive Web Applications" by Packt Publishing, Darie et al. Ch. 2, Page 50 shows ...

In a canvas I created a 2d context. In that context... with a function... I'm able to create some 'circle objects'. Now, what I want, is to get the ImageData of a single circle object instead of the ...

In a canvas I created a 2d context. In that context... with a function... I'm able to create some 'circle objects'. Now, what I want, is to get the ImageData of a single circle object instead of the ...