Skip to content
Advertisement

javascript implementation of lodash “set” method

Found this excellent code for _.get vanilla js implementation:

JavaScript

Now I’m looking for _.set implementation, any help would be appreciated.

Advertisement

Answer

I think this could cover it:

JavaScript

It is a bit more complex than get, because there is some logic needed to create missing parts of the path in the object, to overwrite primitive values that stand in the way, and to determine whether a new child should better be an array or a plain object.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement