[Edit]
+
0
-
0
JavaScript - get nested property value by path (resistant to null/undefined)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17// Optional chaining - ES2020+ solution const getValue = (object, path) => path.reduce((value, key) => value?.[key], object); // Usage example: const myObject = { name: 'John', metadata: { type: 'json', }, }; const metadata = getValue(myObject, ['metadata', 'type']); console.log(metadata); // json
Reset