JSFiddle - React, Tailwind, and code Playground
JavaScript
var Nested = function() {};
// prop is a dot-separated path like "foo.bar.baz"
Nested.prototype.get = function(prop, _default) {
var current = this;
$.each(prop.split("."), function(i, e) {
current = current[e] || undefined;
if (current == undefined)
return false;
});
return current || _default;
}
var a = new Nested();
a.foo = {"bar": {"baz": "Hello World!!!"}};
console.log(a.foo.bar.baz); // Hello World!!!
console.log(a.get("foo.bar.baz", "Won't see me")); // Hello World!!!
console.log(a.get("baz.bar.foo", "Not Found")); // Not found
console.log(a.get("computer.favourite", "Linux")); // Linux