Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 947755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:07:54+00:00 2026-05-15T23:07:54+00:00

With a single property this is fairly easy: var jsonobj = { test: ok

  • 0

With a single property this is fairly easy:


var jsonobj = {
    "test": "ok"
}
var propname = "test";
// Will alert "ok"
alert(jsonobj[propname]);

But what I want to do is use a nested property:


var jsonobj = {
    "test": {
        "test2": "ok"
    }
}
var propname = "test.test2";
// Alerts undefined
alert(jsonobj[propname]);

Is there any way of selecting a nested “dynamic” property?
I know I can do jsonobj.test.test2, but the problem is that propname can change to a property that goes 1,2 or 3 levels deep. (e.g test, test.test2, …)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:07:55+00:00Added an answer on May 15, 2026 at 11:07 pm
    function resolve(cur, ns) {
    
        var undef;
    
        ns = ns.split('.');
    
        while (cur && ns[0])
            cur = cur[ns.shift()] || undef;
    
        return cur;
    
    }
    

    E.g.

    // 1:
    resolve({
        foo: { bar: 123 }
    }, 'foo.bar'); // => 123
    
    
    // 2:
    var complex = {
        a: {
            b: [
                document.createElement('div')
            ]
        }
    };
    
    resolve(complex, 'a.b.0.nodeName'); // => DIV
    

    The benefit in using this is that it won’t throw an error if you try accessing something that doesn’t exist — it’ll gracefully return undefined.


    EDIT:

    In the comment, Andy mentioned that this doesn’t throw errors where one might expect it to. I agree that getting undefined is a little bit generic and there is no way to tell whether your value was really resolved. So, to remedy that, try this:

    var resolve = (function(){
    
        var UNRESOLVED = resolve.UNRESOLVED = {};
        return resolve;
    
        function resolve(cur, ns) {
    
            var undef;
    
            ns = ns.split('.');
    
            while (cur && ns[0])
                cur = cur[ns.shift()] || undef;
    
            if (cur === undef || ns[0]) {
                return UNRESOLVED;
            }
    
            return cur;
    
        }
    
    }());
    

    It’ll return an UNRESOLVED object that can be checked like so:

    var result = resolve(someObject, 'a.b.c');
    
    if (result === resolve.UNRESOLVED) {...}
    

    It’s not perfect, but it is (IMO) the best way to determine an unresolved namespace without having to throw errors. If you want errors, then just go ahead with:

    someObject.a.b.c; //...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 460k
  • Answers 460k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I gave up looking and waiting and wrote my own,… May 15, 2026 at 11:56 pm
  • Editorial Team
    Editorial Team added an answer open a shell, cd to the folder where the file… May 15, 2026 at 11:56 pm
  • Editorial Team
    Editorial Team added an answer Use selection algorithm. Split the array of number to 100… May 15, 2026 at 11:56 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.