import { Callout } from "nextra/components"; import REPL from "../../components/REPL";
Immutable: This does not mutate the original object.Sets the value to an object at the given path.
import { toSet } from '@opentf/std';
toSet<T>(
obj: T,
path: string | unknown[],
value: unknown | ((val: unknown) => unknown)
): T
toSet({}, 'a', null) //=> { a: null }
toSet({}, 'a', 1) //=> { a: 1 }
toSet({}, 'a.b', 25) //=> { a: { b: 25 } }
toSet({}, 'user.email', 'user@example.com')
//=>
// {
// user: { email: 'user@example.com' }
// }
toSet({}, '0', 'Apple') //=> { '0': 'Apple' }
toSet({}, 'fruits[0]', 'Apple') //=> { fruits: ['Apple'] }
toSet({ a: 1 }, 'a', (val) => val + 1) //=> { a: 2 }
const fn = () => render('My Component')
toSet({ subscribeFns: [] }, 'subscribeFns[0]', () => fn)
//=> { subscribeFns: [fn] }
<REPL code={`const { toSet } = require('@opentf/std');
log(toSet({}, 'a.b', 25));
toSet({ a: 1 }, 'a', (val) => val + 1) `} />