
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Accessing nested JavaScript objects with string key
You can use lodash's get method to get properties at any level safely. Getting first-level properties is pretty straightforward. Nested property access is tricky and you should use a tested library like lodash for it.
You can access a deeply nested object in the following way −
Example
let _ = require("lodash"); let obj = { a: { b: { foo: "test" }, c: 2 } }; console.log(_.get(obj, "a.b.foo")); console.log(_.get(obj, "a.c")); console.log(_.get(obj, "a.test")); console.log(_.get(obj, "a.test.x"));
Output
This will give the output −
test 2 undefined undefined
You can also write your own getProp function in the following way −
const getProp = (object, path) => { if (path.length === 1) return object[path[0]]; else if (path.length === 0) throw error; else { if (object[path[0]]) return getProp(object[path[0]], path.slice(1)); else { object[path[0]] = {}; return getProp(object[path[0]], path.slice(1)); } } };
You can use it by passing an array to access the props.
Example
var obj = { level1:{ level2:{ level3:{ name: "Foo" } }, anotherLevel2: "bar" } }; console.log(getProp(obj, ["level1", "level2"]));
Output
This will give the output −
{level3: {name: "Foo"}}
- Related Articles
- Accessing and returning nested array value - JavaScript?
- Safely Accessing Deeply Nested Values In JavaScript
- Merge objects in array with similar key JavaScript
- ES6 Default Parameters in nested objects – JavaScript
- Group objects inside the nested array JavaScript
- Test for existence of nested JavaScript object key in JavaScript
- How to access nested json objects in JavaScript?
- Merge JavaScript objects with the same key value and count them
- Join two objects by key in JavaScript
- Extract key value from a nested object in JavaScript?
- Grouping array nested value while comparing 2 objects - JavaScript
- Convert nested array to string - JavaScript
- Grouping objects based on key property in JavaScript
- Get value for key from nested JSON object in JavaScript
- Accessing Key-value in a Python Dictionary

Advertisements