window.ENTITIES={'/api/snippets/javascript/javascript%20-%20get%20nested%20property%20value%20by%20path%20(resistant%20to%20null%2fundefined)':[{"result":true,"message":null,"batch":{"type":"javascript","name":"javascript - get nested property value by path (resistant to null/undefined)","items":[{"id":"jPN0kD","type":"javascript","name":"JavaScript - get nested property value by path (resistant to null/undefined)","content":"// ONLINE-RUNNER:browser;\n\n// custom function solution when property names don't contain '.'\n\nfunction getValue(object, path) {\n return path.split('.')\n .reduce(function(value, key) {\n return value == null ? value : value[key];\n }, object);\n}\n\n\n// Usage example:\n\nvar myObject = {\n name: 'John',\n metadata: {\n type: 'json',\n },\n};\n\nvar metadata = getValue(myObject, 'metadata');\nvar type = getValue(myObject, 'metadata.type');\n\nconsole.log(metadata); // { type: 'json' }\nconsole.log(type); // json","source":"","author":{"id":"V0JwvD","name":"telsa","avatar":"1629060310048__V0JwvD__w40px_h40px.jpg","points":502,"role":"BASIC"},"creationTime":1666087073000,"updateTime":1699393999000,"removalTime":null},{"id":"13llJD","type":"javascript","name":"JavaScript - get nested property value by path (resistant to null/undefined)","content":"// ONLINE-RUNNER:browser;\n\n// Custom function solution when property names can contain '.'\n\nfunction getValue(object, path) {\n return path.reduce(function(value, key) {\n return value == null ? value : value[key];\n }, object);\n}\n\n\n// Usage example:\n\nvar myObject = {\n name: 'John',\n metadata: {\n type: 'json',\n },\n};\n\nvar metadata = getValue(myObject, ['metadata', 'type']);\n\nconsole.log(metadata); // json","source":"","author":{"id":"V0JqLo","name":"Inayah-Alexander","avatar":"1629131792850__V0JqLo__w40px_h40px.jpg","points":767,"role":"BASIC"},"creationTime":1666087128000,"updateTime":1699394015000,"removalTime":null},{"id":"jQzzQ1","type":"javascript","name":"JavaScript - get nested property value by path (resistant to null/undefined)","content":"// ONLINE-RUNNER:browser;\n\n// Optional chaining - ES2020+ solution\n\nconst getValue = (object, path) => path.reduce((value, key) => value?.[key], object);\n\n\n// Usage example:\n\nconst myObject = {\n name: 'John',\n metadata: {\n type: 'json',\n },\n};\n\nconst metadata = getValue(myObject, ['metadata', 'type']);\n\nconsole.log(metadata); // json","source":"","author":{"id":"VDrPRo","name":"JoanneSenior","avatar":"1629131321922__VDrPRo__w40px_h40px.jpg","points":1070,"role":"BASIC"},"creationTime":1666087185000,"updateTime":1699394024000,"removalTime":null},{"id":"DNllKj","type":"javascript","name":"JavaScript - get nested property value by path (resistant to null/undefined)","content":"// ONLINE-RUNNER:browser;\n\n// Optional chaining (ES2020) with function overloading:\n\nconst getValue$1 = (object, path) => path.reduce((value, key) => value?.[key], object);\nconst getValue$2 = (object, path) => getValue$1(object, path.split('.'));\n\n\n// Usage example:\n\nconst myObject = {\n name: 'John',\n metadata: {\n type: 'json',\n },\n};\n\nconst metadata1 = getValue$1(myObject, ['metadata', 'type']);\nconst metadata2 = getValue$2(myObject, 'metadata.type');\n\nconsole.log(metadata1); // json\nconsole.log(metadata2); // json","source":"","author":{"id":"V0lEjD","name":"Zayyan-Todd","avatar":"1629126291764__V0lEjD__w40px_h40px.jpg","points":860,"role":"BASIC"},"creationTime":1666087354000,"updateTime":1699394031000,"removalTime":null},{"id":"pBVmPp","type":"javascript","name":"JavaScript - get nested property value by path (resistant to null/undefined)","content":"// ONLINE-RUNNER:browser;\n\n// custom function solution when property names don't contain '.'\n\nfunction getValue$1(object, key) {\n return object == null ? object : object[key];\n}\n\nfunction getValue$2(object, path) {\n return path.split('.').reduce(getValue$1, object);\n}\n\n\n// Usage example:\n\nvar myObject = {\n name: 'John',\n metadata: {\n type: 'json',\n },\n};\n\nvar metadata = getValue$2(myObject, 'metadata');\nvar type = getValue$2(myObject, 'metadata.type');\n\nconsole.log(metadata); // { type: 'json' }\nconsole.log(type); // json","source":"","author":{"id":"MoyVe0","name":"Jan-Alfaro","avatar":"1629141121633__MoyVe0__w40px_h40px.jpg","points":711,"role":"BASIC"},"creationTime":1667488472000,"updateTime":1699394037000,"removalTime":null}]}}]};