5

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
  • 16,148
  • 4
  • 74
  • 121
Kunal Shukla
  • 211
  • 1
  • 7
  • 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

3

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
  • 4,118
  • 1
  • 17
  • 21
  • 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
2

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,668
  • 1
  • 11
  • 25
1

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

TPoschel
  • 3,601
  • 2
  • 27
  • 27
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
  • 351,820
  • 24
  • 303
  • 358
-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
  • 485
  • 4
  • 12
-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,402
  • 2
  • 19
  • 35