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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:20:20+00:00 2026-05-24T09:20:20+00:00

i am new to JS objects. I have such code var foo = {

  • 0

i am new to JS objects. I have such code

var foo = {
    bar0: {
        barr0: function() {//doo stuff}
        barr1: function() {//doo stuff}
    },
    bar1: {
        barr0: function() {//do stuff}
        barr1: function() {//do stuff}
    },
    bar2: function() {//do stuff}
}

now i have this variable myVar that holds the ‘name’ of any of the above in the format they would be called. like it can have bar0.barr1 or bar2 or mention to any other of the above objects. foo will not be a part of myVar because it is common for every call. One possible solution is to use eval but i would like to avoid it if possible.

if the object was one dimensional i would have used foo[myVar]() but this is now multidimensional and i hav no idea how should i call it.

  • 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-24T09:20:21+00:00Added an answer on May 24, 2026 at 9:20 am

    You need to apply some scripting then.

    // define our access string and copy a reference from foo into level
    var myVar = 'bar0.barr1',
        level = foo;
    
    // split that access string into an Array (splitted by dots) and loop over it
    myVar.split(/\./).forEach(function( prop ) {
        // if the property name ("bar0", "barr1") is available in our target object
        if( prop in level ) {
            // overwrite our level variable with the new object property reference, so somekind "climb up" the latter if we're dealing with nested objects
            level = level[ prop ];
        }
    });
    
    // if we're done, check if level holds a function reference and if so, execute it.
    if( typeof level === 'function' ) {
        level();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that receives three different people objects and generates a new
Suppose I have this code: var myArray = new Object(); myArray["firstname"] = "Bob"; myArray["lastname"]
I have a new object with a collection of new objects within it on
I have an NSArray and I'd like to create a new NSArray with objects
I'm new to mock objects, but I understand that I need to have my
I have 2 List objects: List<int> lst1 = new List<int>(); List<int> lst2 = new
Trying to do an insert, I have: jdbcTemplate.update(insert into...., new Object[]{foo.getId(), foo.getName()}) foo.getId() returns
I'd like to have JavaScript objects within another JavaScript object as such: Issues: -
I have set my data provider as Bindable as such: [Bindable] public var dataProvider:ArrayCollection;
I have an object that I want to be constructed in such manner: var

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.