setByPath
オブジェクト obj
とフィールドパス path
を入力し、パスに従ってオブジェクトを深くトラバースして、値 value
を設定します。
Added in v0.0.1
Usage
ts
import { setByPath } from 'parsnip-kit'
const test0 = {}
setByPath(test0, 'a', 1) // { a: 1 }
const test1 = []
setByPath(test1, '[0]', 1) // [1]
const test2 = {}
setByPath(test2, '[0]', 1) // { 0: 1 }
const test3 = {}
setByPath(test3, 'b[2]', 2) // { b: [<empty>, <empty>, 2] }
const test4 = { a: 1 }
setByPath(test4, 'a[0]', 'test') // { a: ['test'] }
const test5 = { a: 1 }
setByPath(test5, 'b', 2) // { a: 1, b: 2 }
API
Arguments
Arg | Type | Optional | Default | Description |
---|---|---|---|---|
obj | object | false | undefined | 設定するオブジェクト |
path | string | false | undefined | フィールドパス |
value | any | false | undefined | 設定する値 |
Returns
Type |
---|
void |