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 8083285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:24:50+00:00 2026-06-05T17:24:50+00:00

I have an variable obj defined as follows: {user: {username: AzureDiamond, password: hunter2}} I

  • 0

I have an variable obj defined as follows:

{user: {username: "AzureDiamond", password: "hunter2"}}

I have a string str that might be defined as any of the following strings:

  • “user”
  • “user[username]”
  • “user[password]”
  • “fake”

Is there a relatively easy way for me in JS (specifically node.js) to essentially/dynamically perform the following?

  • when str == "user", return {username: "AzureDiamon", password: "hunter2"}
  • when str == "user[username]", return “AzureDiamond”
  • when str == "user[password]", return “hunter2”
  • when str == "fake", return null

Edit: This question is more to find out if there’s anything easier or build into JS/node.js that I can use other than regex matches.

  • 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-05T17:24:51+00:00Added an answer on June 5, 2026 at 5:24 pm

    Solution

    If you are sure the syntax of str is correct, the function could look like this:

    function get_by_path(source, path){
        var path_elements = path.split(']').join('').split('[');
        for (var i=0; i < path_elements.length; i++){
            source = source[path_elements[i]];
            if (typeof source === 'undefined'){
                return null;
            }
        };
        return source;
    };
    

    Test

    This is how it behaves:

    var data = {'user': {
        'username': "AzureDiamond", 'password': "hunter2",
        'details': {'address': {'street': "Happiness Street"}}
    }};
    
    get_by_path(data, 'user');                           // gives user object
    get_by_path(data, 'user[username]');                 // gives "hunter2"
    get_by_path(data, 'user[password]');                 // gives "AzureDiamond"
    get_by_path(data, 'fake');                           // gives null
    get_by_path(data, 'user[details][address][street]'); // gives "Happiness Street"
    

    Explanation

    The script does not use regular expression, nor dangerous eval() calls, only assumes your “path” will start with one word without brackets and every next part (if any) will be word enclosed in square brackets. If some path will not be found within the traversed object, null will be returned. If it will be found, it will be returned (regardless of whether it will be some complex object, string, null, boolean or anything else).

    Script begins with parsing your path. It does that by removing closing square brackets (“]“) and splitting resulting string by opening square brackets (“[“). The process looks like this:

    "user"                  -> "user"                -> ["user"]
    "user[username]"        -> "user[username"       -> ["user", "username"]
    "user[password]"        -> "user[password"       -> ["user", "password"]
    "user[address][street]" -> "user[address[street" -> ["user", "address", "street"]
    "fake"                  -> "fake"                -> ["fake"]
    

    So, as you see, at the end you have an array of path elements. These are evaluated one by one. If at any level appropriate step cannot be made deeper into the structure of the data argument, then null is returned. Otherwise the last accessed element is returned.

    Proof

    Proof that it works is here: http://jsfiddle.net/EJCgE/2/

    EDIT: There was some issue with more complex paths (when there were more than two levels), resulting from how string’s replace() method works. I have updated my code to not include jQuery and to fix that issue.

    EDIT2: I have updated the script to remove redundant lines and one redundant variable.

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

Sidebar

Related Questions

If I have a variable containing a string, is there a way that I
i have variable $lang which convert string to the selected language but i am
I have a variable foo that contains a time, lets say 4pm today, but
I have a variable in my xsl that may or may not be declared.
I have a variable that im define with another variable and some text. $title
I have a variable containing a string. The string can come in several ways:
I have a variable that can either contain a list of strings or a
I have a global variable: const std::string whiteSpaceBeforeLeadingCmntOption = WhiteSpaceBeforeLeadingComment; When I remove the
I have javascript variable obj which represents an element in the DOM and I
I have some code that assigns an object to a generic id variable and

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.