Skip to main content
deleted 32 characters in body
Source Link
amrender singh
  • 8.3k
  • 4
  • 27
  • 30

Since a and b in your case contains a property with same name, when you do something like Object.assign(a,b)the prop property of b overwrites and not merge the prop. Reference

Try the following:

var a = {prop:{a:'1'}}
var b = {prop:{b:'1'}}

var c = {};
c.prop = Object.assign({},a.prop,b.prop);

console.log(c);

Or

You can write your own custom function which can merge the values of the objects with same properties.

Try the following:

function merge(source, target) {
    Object.keys(source).forEach((k) => {
        if (source[k].constructor.toString().indexOf('Object') > -1) {          
 && target[k]) {        target[k] = target[k] || {};
            merge(source[k], target[k]);
            return;
        }
        target[k] = source[k];
    });
    return target;
}

var a = {prop:{a: '1'}};
var b = {prop:{ b: '1'}};
var c = {};
[a,b].forEach((e)=>merge(e, c));
    
console.log(c);

Since a and b in your case contains a property with same name, when you do something like Object.assign(a,b)the prop property of b overwrites and not merge the prop. Reference

Try the following:

var a = {prop:{a:'1'}}
var b = {prop:{b:'1'}}

var c = {};
c.prop = Object.assign({},a.prop,b.prop);

console.log(c);

Or

You can write your own custom function which can merge the values of the objects with same properties.

Try the following:

function merge(source, target) {
    Object.keys(source).forEach((k) => {
        if (source[k].constructor.toString().indexOf('Object') > -1) {          
           target[k] = target[k] || {};
            merge(source[k], target[k]);
            return;
        }
        target[k] = source[k];
    });
    return target;
}

var a = {prop:{a: '1'}};
var b = {prop:{ b: '1'}};
var c = {};
[a,b].forEach((e)=>merge(e, c));
    
console.log(c);

Since a and b in your case contains a property with same name, when you do something like Object.assign(a,b)the prop property of b overwrites and not merge the prop. Reference

Try the following:

var a = {prop:{a:'1'}}
var b = {prop:{b:'1'}}

var c = {};
c.prop = Object.assign({},a.prop,b.prop);

console.log(c);

Or

You can write your own custom function which can merge the values of the objects with same properties.

Try the following:

function merge(source, target) {
    Object.keys(source).forEach((k) => {
        if (source[k].constructor.toString().indexOf('Object') > -1 && target[k]) {          
            merge(source[k], target[k]);
            return;
        }
        target[k] = source[k];
    });
    return target;
}

var a = {prop:{a: '1'}};
var b = {prop:{ b: '1'}};
var c = {};
[a,b].forEach((e)=>merge(e, c));
    
console.log(c);

added 798 characters in body
Source Link
amrender singh
  • 8.3k
  • 4
  • 27
  • 30

Since a and b in your case contains a property with same name, when you do something like Object.assign(a,b)the prop property of b overwrites and not merge the prop. Reference

Try the following:

var a = {prop:{a:'1'}}
var b = {prop:{b:'1'}}

var c = {};
c.prop = Object.assign({},a.prop,b.prop);

console.log(c);

Or

You can write your own custom function which can merge the values of the objects with same properties.

Try the following:

function merge(source, target) {
    Object.keys(source).forEach((k) => {
        if (source[k].constructor.toString().indexOf('Object') > -1) {          
           target[k] = target[k] || {};
            merge(source[k], target[k]);
            return;
        }
        target[k] = source[k];
    });
    return target;
}

var a = {prop:{a: '1'}};
var b = {prop:{ b: '1'}};
var c = {};
[a,b].forEach((e)=>merge(e, c));
    
console.log(c);

Since a and b in your case contains a property with same name, when you do something like Object.assign(a,b)the prop property of b overwrites and not merge the prop. Reference

Try the following:

var a = {prop:{a:'1'}}
var b = {prop:{b:'1'}}

var c = {};
c.prop = Object.assign({},a.prop,b.prop);

console.log(c);

Since a and b in your case contains a property with same name, when you do something like Object.assign(a,b)the prop property of b overwrites and not merge the prop. Reference

Try the following:

var a = {prop:{a:'1'}}
var b = {prop:{b:'1'}}

var c = {};
c.prop = Object.assign({},a.prop,b.prop);

console.log(c);

Or

You can write your own custom function which can merge the values of the objects with same properties.

Try the following:

function merge(source, target) {
    Object.keys(source).forEach((k) => {
        if (source[k].constructor.toString().indexOf('Object') > -1) {          
           target[k] = target[k] || {};
            merge(source[k], target[k]);
            return;
        }
        target[k] = source[k];
    });
    return target;
}

var a = {prop:{a: '1'}};
var b = {prop:{ b: '1'}};
var c = {};
[a,b].forEach((e)=>merge(e, c));
    
console.log(c);

added 1 character in body
Source Link
amrender singh
  • 8.3k
  • 4
  • 27
  • 30

Since a and b in your case contains a property with same name, when you do something like Object.assign(a,b)the prop property of b overridesoverwrites and not merge the prop. Reference

Try the following:

var a = {prop:{a:'1'}}
var b = {prop:{b:'1'}}

var c = {};
c.prop = Object.assign({},a.prop,b.prop);

console.log(c);

Since a and b in your case contains a property with same name, when you do something like Object.assign(a,b)the prop property of b overrides and not merge the prop. Try the following:

var a = {prop:{a:'1'}}
var b = {prop:{b:'1'}}

var c = {};
c.prop = Object.assign({},a.prop,b.prop);

console.log(c);

Since a and b in your case contains a property with same name, when you do something like Object.assign(a,b)the prop property of b overwrites and not merge the prop. Reference

Try the following:

var a = {prop:{a:'1'}}
var b = {prop:{b:'1'}}

var c = {};
c.prop = Object.assign({},a.prop,b.prop);

console.log(c);

added 169 characters in body
Source Link
amrender singh
  • 8.3k
  • 4
  • 27
  • 30
Loading
Source Link
amrender singh
  • 8.3k
  • 4
  • 27
  • 30
Loading