Sets the value to an object at the given path.
⚠️
This mutates the original object.
Related
Syntax
import { set } from '@opentf/std';
set<T>(
obj: T,
path: string | unknown[],
value: unknown | ((val: unknown) => unknown)
): T
The value param can be either any value
or callback
function.
The callback
fn can be called with the property path value if it exist.
Examples
set({}, 'a', null) //=> { a: null }
set({}, 'a', 1) //=> { a: 1 }
set({}, 'a.b', 25) //=> { a: { b: 25 } }
set({}, 'user.email', 'user@example.com')
//=>
// {
// user: { email: 'user@example.com' }
// }
set({}, '0', 'Apple') //=> { '0': 'Apple' }
set({}, 'fruits[0]', 'Apple') //=> { fruits: ['Apple'] }
set({ a: 1 }, 'a', (val) => val + 1) //=> { a: 2 }
const fn = () => render('My Component')
set({ subscribeFns: [] }, 'subscribeFns[0]', () => fn)
//=> { subscribeFns: [fn] }