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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:51:30+00:00 2026-05-27T17:51:30+00:00

Not sure how to word this. I am trying to use a variable to

  • 0

Not sure how to word this.

I am trying to use a variable to determine how far to drill into an object to return a value.

var target = "level1.level2.var";
var myObject = {
                    level1: {
                        level2: {
                            var: 'value'
                        }  
                    }
               }
var returnVal = myObject.target;

How could this be done? Clearly this won’t work. Is it possible some other way?

I figured I would have to maybe explode the target var and then loop for each level, but thought I’d ask to see if there was any easier way I could be overlooking.

  • 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-27T17:51:31+00:00Added an answer on May 27, 2026 at 5:51 pm

    You could use this function:

    function get_property_from_target(obj, target){
        var arr = target.split('.');
        for(var i = 0; i < arr.length; i++){
            if(obj)
                obj = obj[arr[i]];
        }
        return obj;
    }
    

    Then call it like:

    get_property_from_target(myObject, target);
    

    I’d rename the function to something better too.

    Also please don’t name an objects property var since that is a keyword in Javascript it can be confusing, and I’m not sure that it will always work the way you expect or if it will just cause errors in some browsers.

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

Sidebar

Related Questions

Apologize if this is duplicate - not sure how to word what Im trying
I'm not sure how to word this cause I am a little confused at
Not sure exactly how to word this question ... so edits are welcomed! Anyway
Not quite sure how to word this question. I am wondering if there is
Not really too sure how to word this question, therefore if you don't particularly
I'm not entirely sure if this is the best way to word it, however,
Not sure how to word the question... Basically, so far all my SQL stuff
I have a question but I'm not sure of the word to use. My
I'm not exactly sure how to word this in English, but I want to
Sorry if my title is not clear, I'm not sure how to word this.

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.