{"ScriptPreparationCode":"var person = {name: \u0027Frederick\u0027, lastName: \u0027Corcino Alejo\u0027, nested: {name: \u0027test\u0027}};\r\n\r\nfunction safeGet(obj, path, defaultValue) {\r\n return path.split(\u0027.\u0027).reduce((xs, x) =\u003E (xs \u0026\u0026 xs[x]) ? xs[x] : defaultValue, obj);\r\n}\r\n\r\nfunction get(obj, path, defValue) {\r\n // If path is not defined or it has false value\r\n if (!path) return undefined\r\n // Check if path is string or array. Regex : ensure that we do not have \u0027.\u0027 and brackets.\r\n // Regex explained: https://regexr.com/58j0k\r\n const pathArray = Array.isArray(path) ? path : path.match(/([^[.\\]])\u002B/g)\r\n // Find value\r\n const result = pathArray.reduce(\r\n (prevObj, key) =\u003E prevObj \u0026\u0026 prevObj[key],\r\n obj\r\n )\r\n // If found value is undefined return default value; otherwise return the value\r\n return result === undefined ? defValue : result\r\n}\r\n\r\n// Another \u0022get\u0022 implementation\r\n// https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_get\r\nfunction youDontNeedGet(obj, path, defaultValue = undefined) {\r\n const travel = regexp =\u003E\r\n String.prototype.split\r\n .call(path, regexp)\r\n .filter(Boolean)\r\n .reduce((res, key) =\u003E (res !== null \u0026\u0026 res !== undefined ? res[key] : res), obj);\r\n const result = travel(/[,[\\]]\u002B?/) || travel(/[,[\\].]\u002B?/);\r\n return result === undefined || result === obj ? defaultValue : result;\r\n};","TestCases":[{"Name":"Lodash get","Code":"_.get(person, \u0027name\u0027, \u0027\u0027);\r\n_.get(person, \u0027nested.name\u0027, \u0027\u0027);","IsDeferred":false},{"Name":"safeGet","Code":"safeGet(person, \u0027name\u0027, \u0027\u0027);\r\nsafeGet(person, \u0027nested.name\u0027, \u0027\u0027);","IsDeferred":false},{"Name":"optional chaining","Code":"person?.name;\r\nperson?.nested?.name;","IsDeferred":false},{"Name":"get","Code":"get(person, \u0027name\u0027, null)\r\nget(person, \u0027nested.name\u0027, null)","IsDeferred":false},{"Name":"youDontNeedGet","Code":"youDontNeedGet(person, \u0027name\u0027, null)\r\nyouDontNeedGet(person, \u0027nested.name\u0027, null)","IsDeferred":false}]}