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

  • SEARCH
  • Home
  • 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 9147913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:07:11+00:00 2026-06-17T11:07:11+00:00

I’m finding myself struggling with a little problem. Let’s say I’ve got an object:

  • 0

I’m finding myself struggling with a little problem.
Let’s say I’ve got an object:

var foo = {
    bar: {
        baz: true
    }
};

Now I also have a String 'foo.bar.baz'. I’d now like to retrieve the value from the object using the string.

Please note: This is just an example, the solution needs to be dynamic.

Update:

I need the variable name also to be dynamic and parsed from the string. Also I can’t be sure that my variable is a property of the window.

I have already built a solution using eval, but this is pretty ugly I think:
http://jsfiddle.net/vvzyX/

  • 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-06-17T11:07:12+00:00Added an answer on June 17, 2026 at 11:07 am

    Here is how you can do this:

    function getValue(namespace, parent) {
        var parts = namespace.split('.'),
            current = parent || window;
        for (var i = 0; i < parts.length; i += 1) {
            if (current[parts[i]]) {
                current = current[parts[i]];
            } else {
              if (i >= parts.length - 1)
                return undefined;
            }
        }
        return current;
    }
    var foo = {
        bar: {
            baz: true
        }
    };
    console.log(getValue('foo.bar.baz')); //true
    

    The first argument of the function is the namespace (dot separated values) and the second one is the parent object, if parent is not provided then window is used.

    One more example using the parent argument:

    var str = 'foo.bar.baz';
        foo = {
           bar: {
              baz: true
           }
        };
    
    result = getValue(str, foo);
    console.log(result);
    

    Here is an example in jsfiddle.

    Similar approach is used in YUI. Their approach is called Namespace pattern. The main benefit is simulation of packages/namespaces. The only difference between this script and the namespace pattern is that the namespace function creates nested structure instead of only returning value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
i got an object with contents of html markup in it, for example: string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have been unable to fix a problem with Java Unicode and encoding. The
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.