mirror of
https://github.com/cp6/my-idlers.git
synced 2025-04-21 18:48:37 +00:00
20 lines
No EOL
432 B
JavaScript
20 lines
No EOL
432 B
JavaScript
'use strict';
|
|
|
|
/**
|
|
* Get value of a nested property
|
|
*
|
|
* @param mainObject
|
|
* @param key
|
|
* @returns {*}
|
|
*/
|
|
|
|
module.exports = function nestedValue(mainObject, key) {
|
|
try {
|
|
return key.split('.').reduce(function (obj, property) {
|
|
return obj[property];
|
|
}, mainObject);
|
|
} catch (err) {
|
|
// If we end up here, we're not working with an object, and @var mainObject is the value itself
|
|
return mainObject;
|
|
}
|
|
}; |