access-by-path
v0.1.1
Published
Get and set values in object by path
Downloads
4
Readme
access-by-path
Functions of getting and setting the values of the object fields by the string representation of the path to them.
Install
npm install access-by-path
Usage
getByPath
import { getByPath } from 'access-by-path';
let obj = { a: 1, b: { c: 2, d: 3}};
let result = getByPath(obj, 'b.d'); // 3
getByPath(obj, path[, defaultValue[, splitChar[, escapeChar]]])
obj {Object}
- objectpath {string}
- path to fielddefaultValue {*} [undefined]
- return value if the field is not found in objectsplitChar {string} ['.']
- path delimiter characterescapeChar {string} ['\\']
- character to escape delimiter
setByPath
import { setByPath } from 'access-by-path';
let obj = { a: 1, b: { c: 2, d: 3}};
setByPath(obj, 'b.d', 4); // { a: 1, b: { c: 2, d: 4}}
setByPath(obj, path, value[, splitChar[, escapeChar]])
obj {Object}
- objectpath {string}
- path to fieldvalue {*}
- set valuesplitChar {string} ['.']
- path delimiter characterescapeChar {string} ['\\']
- character to escape delimiter
Testing
To run tests, use:
npm test