Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge two objects in javascript ignoring undefined values

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' }
like image 203
Kunal Shukla Avatar asked Apr 28 '26 00:04

Kunal Shukla


1 Answers

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

like image 64
TPoschel Avatar answered Apr 30 '26 15:04

TPoschel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!