Search…
Util.paths Module
Import
1
import { paths } from 'util';
Copied!

Methods

to(object, path, value, returnSourceObject)

Search object for a path using a dot syntax and returns a value. If the value argument is present, the function writes the path to the object and returns the inner most object match, creating objects when none are present.
Arguments
  • object (Object) The object to search or update.
  • path (String) The path to search or update. Sub-properties can be found/updated using a . as a separator.
  • value (*) If present, writes to the innermost path. If any of the properties are missing from the path, they are created as plain javascript objects.
  • returnSourceObject (Boolean) If true, returns object instead of the innermost property.
Returns
JavaScript
1
import { paths } from 'util';
2
3
const object = {
4
foo: [
5
0,
6
1,
7
{two: 'two'}
8
]
9
};
10
11
paths.to(object, 'not.a.property'); // undefined
12
paths.to(object, 'foo.2.two'); // 'two'
13
paths.to({}, 'a.b.c', 'd'); // {c: 'd'}
14
paths.to({}, 'a.b.c', 'd', true); // {a: {b: {c: 'd'}}}
Copied!
Last modified 2mo ago