9

I have to merge two objects but I don't want to assign undefined value to defined values.

A = { activity: 'purchased', count: undefined, time: '09:05:33' }
B = { activity: 'purchased', count: '51', time: undefined }

When I try Object.assign, undefined is replacing fields that have values.

What I want

C = { activity: 'purchased', count: '51', time: '09:05:33' }
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Kunal Shukla
  • 311
  • 5
  • 10
  • 1
    first remove the undefined `Object.keys(A).filter(key => A[key]).reduce((acc, key) => (acc[key] = A[key], acc), {})` ones and assign. – Kharel Jul 10 '20 at 16:02
  • Will `A` and `B` have same property name (i.e no extra property in any of them) and can `A` and `B` have different values of same property? If yes, them which one to assign. – kapil pandey Jul 10 '20 at 16:08
  • I cleaned up the tags but kept the _reactjs_ tag as I guess it's because you're looking for an immutable solution? – Emile Bergeron Jul 10 '20 at 17:59

6 Answers6

4

The spread operator(...) works well to merge objects, and there is a simple solution to remove undefined using JSON.stringify() and JSON.parse(). See below example:

const A = { activity: 'purchased', count: undefined, time: '09:05:33' };
const B = { activity: 'purchased', count: '51', time: undefined };

//If you don't care about date objects then only use below method 
const C = {...JSON.parse(JSON.stringify(A)), ...JSON.parse(JSON.stringify(B))};

console.log(C);
Arun Saini
  • 6,714
  • 1
  • 19
  • 22
  • 1
    This won't work with a variety of other values ([anything that's not JSON serializable](https://stackoverflow.com/a/122704/1218980)), so be careful. – Emile Bergeron Jul 10 '20 at 17:58
3

let A = { activity: 'purchased', count: undefined, time: '09:05:33' }
let B = { activity: 'purchased', count: '51', time: undefined }

let C={}
Object.keys({...A,...B}).map(key=>{
C[key]=B[key]||A[key]
})
console.log(C)
kapil pandey
  • 1,853
  • 1
  • 13
  • 26
3

You could use lodash's merge command. C = _.merge(A,B);

TPoschel
  • 3,775
  • 2
  • 30
  • 29
0

You could merge the objects by having a look to the entries of the second object and take only key/value pairs without undefined as value.

const
    merge = (a, b) => Object.assign(
        {},
        a,
        ...Object.entries(b).map(([k, v]) => v === undefined ? {} : { [k]: v })
    ),
    a = { activity: 'purchased', count: undefined, time: '09:05:33' },
    b = { activity: 'purchased', count: '51', time: undefined };

console.log(merge(a, b));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
-2
const A = { activity: 'purchased', count: undefined, time: '09:05:33' }
const B = { activity: 'purchased', count: '51', time: undefined }
const AKeys = Object.keys(A);
const BKeys = Object.keys(B);
const C = {};
AKeys.forEach(element=>A[element] && C[element]=A[element])
BKeys.forEach(element=>B[element] && C[element]=B[element])
MadPapo
  • 495
  • 4
  • 13
-2

let A = { activity: 'purchased', count: undefined, time: '09:05:33' }
let B = { activity: 'purchased', count: '51', time: undefined }

for (let a in A) {
  if (A[a] === undefined) 
    delete A[a];
}

for (let b in B) {
  if (B[b] === undefined) 
    delete B[b];
}

let c = {...A, ...B} // this will merge identical key/values

console.log(c)
fedesc
  • 2,554
  • 2
  • 23
  • 39