DEV Community

Discussion on: How to Traverse an Object of Arbitrary Depth in JavaScript

Collapse
 
256hz profile image
Abe Dolinger • Edited

A quick and dirty way is to stringify your object and perform a regex replace:

const newApplication = JSON.parse(JSON.stringify(application).replace('[tbd]', 'Daffodil'))

It's likely this is more performant as well, but I haven't tested it. It also introduces the possibility of some nasty bugs if your new input isn't sanitized in some way, so your method is certainly better in that regard.

Collapse
 
nas5w profile image
Nick Scialli (he/him)

But this fails quite readily if any part of your object isn't stringifiable as JSON!

Collapse
 
256hz profile image
Abe Dolinger

Also true! Just works for your example