JSFiddle - React, Tailwind, and code Playground

by Ilya

JavaScript

/* const getValue = function(object, propertyName) {
  console.log(object, propertyName)
  return typeof object === 'undefined' ? undefined : object[propertyName]
} */

const getNestedValue = function(object, propName) {
	return propName.split('.').reduce(function(object, propertyName) {
	
	return typeof object === 'undefined' ? undefined : object[propertyName]
}, object)
}

console.log(getNestedValue({bar: 'baz'}, 'bar') === 'baz')
console.log(getNestedValue({bar: {baz: 1}}, 'bar.baz') === 1)