{"ScriptPreparationCode":"var state = {\r\n wrapped1: {\r\n wrapped2: {\r\n location: {\r\n \tsearch: \u0027\u0027,\r\n \tid: 12\r\n }\r\n }\r\n },\r\n data: {}\r\n};\r\n\r\nvar safeGet = function(nestedObj, paths, defaultVal) {\r\n if (!paths || paths.length === 0) return defaultVal\r\n const result = paths.slice(0).reduce((obj, path, i, arr) =\u003E {\r\n if (!obj) {\r\n arr.splice(i) // break out of the loop\r\n return undefined\r\n }\r\n return obj[path]\r\n }, nestedObj)\r\n\r\n if (result === undefined || result === null) {\r\n return defaultVal\r\n }\r\n\r\n return result\r\n}\r\nvar safeGet2 = function(nestedObj, paths, defaultVal) {\r\n if (!paths || paths.length === 0) return defaultVal\r\n for (let p = 0; p \u003C paths.length; p\u002B\u002B) {\r\n nestedObj = nestedObj ? nestedObj[paths[p]] : undefined\r\n }\r\n return nestedObj === undefined || nestedObj === null ? defaultVal : nestedObj\r\n}\r\nvar safeGet3 = function(nestedObj, paths, defaultVal, p, undef) {\r\n if (!paths || paths.length === 0) return defaultVal\r\n for (p = 0; p \u003C paths.length; p\u002B\u002B) {\r\n nestedObj = nestedObj ? nestedObj[paths[p]] : undef\r\n }\r\n return nestedObj === undef || nestedObj === null ? defaultVal : nestedObj\r\n}\r\n\r\nvar dlv = function(obj, key, def, p, undef) {\r\n\tkey = key.split ? key.split(\u0027.\u0027) : key;\r\n\tfor (p = 0; p \u003C key.length; p\u002B\u002B) {\r\n\t\tobj = obj ? obj[key[p]] : undef;\r\n\t}\r\n\treturn obj === undef ? def : obj;\r\n}","TestCases":[{"Name":"old safeGet","Code":"var l = safeGet(state, [\u0027wrapped1\u0027,\u0027wrapped2\u0027,\u0027location\u0027,\u0027search\u0027]);","IsDeferred":false},{"Name":"dlv (array notation)","Code":"dlv(state, [\u0027wrapped1\u0027,\u0027wrapped2\u0027,\u0027location\u0027,\u0027search\u0027]);","IsDeferred":false},{"Name":"safeGet NOT using variables as params","Code":"safeGet2(state, [\u0027wrapped1\u0027,\u0027wrapped2\u0027,\u0027location\u0027,\u0027search\u0027]);","IsDeferred":false},{"Name":"safeGe using variables as params (this PR code)","Code":"safeGet3(state, [\u0027wrapped1\u0027,\u0027wrapped2\u0027,\u0027location\u0027,\u0027search\u0027]);","IsDeferred":false}]}