Safely setting object properties with dot notation strings in JavaScript
obj = typeof obj === 'object' ? obj : {};


When obj argument points to something other than object you will create one. Like in set("foo", "a.b.c", "Yay!"). I guess you want to return it at function end or it will be lost.

Other notes:


/^\+?(0|[1-9]\d*)$/ allows + in integer key inputs
you may use isNaN(+nextKey) instead of regular expression, but beware float or signed numbers
finalStep is a bit misleading name. You may use key = keys[keys.length - 1] instead.