nestedValue.js 431 B

12345678910111213141516171819
  1. 'use strict';
  2. /**
  3. * Get value of a nested property
  4. *
  5. * @param mainObject
  6. * @param key
  7. * @returns {*}
  8. */
  9. module.exports = function nestedValue(mainObject, key) {
  10. try {
  11. return key.split('.').reduce(function (obj, property) {
  12. return obj[property];
  13. }, mainObject);
  14. } catch (err) {
  15. // If we end up here, we're not working with an object, and @var mainObject is the value itself
  16. return mainObject;
  17. }
  18. };