JSFiddle - React, Tailwind, and code Playground
HTML
<label>Enter property names, separated by spaces or dots:<br/><input/></label>
<button>Go</button>
<div id="result"></div>
CSS
body { font-family: sans-serif; }
JavaScript
function TryGetPropertyValue(o, propertyName1 /*, ... propertyNameN */ ) {
var names = [].slice.call(arguments, 1);
while (o && names.length) {
o = o[names.shift()];
}
if (names.length == 0) {
return o;
}
}
var obj = {
foo: 1,
bar: {
baz: {
fax: 1,
fuz: 2,
nil: null
},
zip: null,
yep: true,
yah: "OK",
nop: false,
und: function(){}()
}
};
$(function() {
$("input").on("keypress change", function(e) {
if (e.type == "change" || e.keyCode == 13) {
var args = [obj].concat(this.value.split(/[\s.]+/));
$("#result").text(TryGetPropertyValue.apply(null, args) + "");
}
}).val("bar.baz.fuz").change().focus();
});