156. implement `_.set()`
Share
medium - accepted / - tried
_.set(object, path, value) is a handy method to updating an object without checking the property existence.
Can you create your own set()
?
const obj = { a: { b: { c: [1,2,3] } }}set(obj, 'a.b.c', 'BFE')console.log(obj.a.b.c) // "BFE"set(obj, 'a.b.c.0', 'BFE')console.log(obj.a.b.c[0]) // "BFE"set(obj, 'a.b.c[1]', 'BFE')console.log(obj.a.b.c[1]) // "BFE"set(obj, ['a', 'b', 'c', '2'], 'BFE')console.log(obj.a.b.c[2]) // "BFE"set(obj, 'a.b.c[3]', 'BFE')console.log(obj.a.b.c[3]) // "BFE"set(obj, 'a.c.d[0]', 'BFE')// valid digits treated as array elementsconsole.log(obj.a.c.d[0]) // "BFE"set(obj, 'a.c.d.01', 'BFE')// invalid digits treated as property stringconsole.log(obj.a.c.d['01']) // "BFE"