As anyone who knows me can tell you, I dislike dependencies - a lot. Over my years of experience, dependencies have, on occasion, caused more problems than they've solved, and they have certainly caused more than a few sleepless nights.
Finding and retrieving a property, however, is a rather simple task as long as the path to the property can be specified in dot notation. Even more, this can be added to the JavaScript Object prototype easily and, as part of ES5, is supported by nearly all browsers...except Internet Explorer 8.
Here's the code you'll need - it's shown here as a method on a specific object, but I'll leave it up to you exactly how to implement it.
First, we'll create an object.
JavaScript
Next we add our find method to it.
JavaScript find method
What this function is doing is relatively straightforward. It takes the
path
specified
and splits it apart using the dots in the dot notation. It then loops through the array of path segments
and shifts the reference point at each step, assuming the reference point actually holds a value. By
adding the this
keyword as the initial value of the reduce
method, we eliminate
the need for any external references. If the path is not found, and a default value is provided in the
value
parameter, the default value is returned, otherwise, undefined
is returned.With this
find
function, you can get a property value by specifying the path like so...
myObject.find('situation.normal.afu'); // returns true
. Any properties requested that are undefined,
even if they are nested inside undefined properties, are returned as undefined. For example, with our object,
myObject.find('situation.normal.nafu.ok')
will return undefined without throwing a TypeError
as would typically happen when 'ok' could not be read from the undefined 'nafu' property.Hopefully this will simplify your code and reduce those dependencies.
Happy coding.
No comments:
Post a Comment