Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
How do you construct an object that behaves like a lazily-evaluated deep merge of two other objects in JavaScript?

How do you write a function lazyDeepMerge(object1, object2) which constructs and returns some object lazyObject such that whenever lazyObject is queried for a the value of a property someProp, the value returned behaves exactly the same as the value returned by calling regularDeepMerge(object1, object2).someProp?

As background, deep merging two objects (recursively merging their respective properties) has been addressed here: How to deep merge instead of shallow merge?

The difference between lazyDeepMerge and regular deepMerge() would be that the lazy one would not do any merging of values in advance.

For example, calling lazyObject.vehicles.camry.color would merge what’s needed in order to return a value identical to the one returned from calling eagerObject.vehicles.camry.color.

The process would be that calling lazyObject.vehicles would return an object lazyVehicles. If you then called lazyVehicles.camry.color, lazyVehicles would lazily merges what's necessary to return the value, and that value may be another lay object, etc.

Any solution would probably have to make use of the JavaScript Proxy object, which is very straightforward to use - so that’s not an issue I need to solve.

An ideal solution would involve caching the values of properties the first time they’re looked up, as well as the ability to pass in a custom merge(obj1, obj2) function (like with the Lodash library function _.mergeWith(object, sources, customizer))

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!