string.getObject
string.getObject(name, roots)
Parameters
- name
{String}
:a String of dot-separated keys, representing a path of properties
- roots
{Object|Array}
:the object to use as the root for property based navigation
Returns
{*}
:
the value at the property path descending from roots
Return the result of descending the path name
through the properties of the object or objects
roots
If roots
is an Array, each element of the array is evaluated, in order, until
the path is found in an element's properties (and properties-of-properties, etc.). Otherwise
roots
is evaluated as the root object, returning either the object at the property path
descended from roots
or undefined
if any subpath is not found.
A path is a dot-delimited sequence of zero or more property names, such that "foo.bar" means "the property
'bar' of the object at the property 'foo' of the root." An empty path returns the first object in roots
if it's an array, roots
itself otherwise.
var string = require("can-util/js/string/string");
console.log(string.getObject("a.b.c", {a: {b: {c: "foo"}}})); // -> "foo"
console.log(string.getObject("a.b.c", {a: {}})); // -> undefined
console.log(string.getObject("a.b", [{a: {}}, {a: {b: "bar"}}])); // -> "bar"