Util.paths Module

Import

import { paths } from 'util';

Methods

to(object, path, value, returnSourceObject)

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

import { paths } from 'util';

const object = {
  foo: [
    0,
    1,
    {two: 'two'}
  ]
};

paths.to(object, 'not.a.property'); // undefined
paths.to(object, 'foo.2.two'); // 'two'
paths.to({}, 'a.b.c', 'd'); // {c: 'd'}
paths.to({}, 'a.b.c', 'd', true); // {a: {b: {c: 'd'}}}

Last updated