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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:31:44+00:00 2026-06-18T04:31:44+00:00

Possible Duplicate: Accessing nested JavaScript objects with string key I have the function function

  • 0

Possible Duplicate:
Accessing nested JavaScript objects with string key

I have the function

function _get(name) {
return plugin._optionsObj[name] !== undefined ? 
    plugin._optionsObj[name] : plugin._defaults[name];
}

I would like to be able to have objects inside of my _defaults object, but then I don’t know how to retrieve them but using just one set of square brackets.

i.e.

plugin._defaults = {
    val1: 1,
    val2: 2,
    obj1: {
        someVal: 3
    }
}

Is it possible to access ‘someVal’ from the function I have above? I tried passing ‘obj1.someVal’ for the argument and it didn’t work. Ideas?

Edit: I have found a solution and I posted it below as an answer. I’ve written a very nice little function to do go through the nested values with a string and I didn’t have to change my function much to implement it. I hope this helps anyone in a similar situation.

  • 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-18T04:31:45+00:00Added an answer on June 18, 2026 at 4:31 am

    I found a solution for this problem, at least one that will accommodate myself, and I’d like to share it in case it can help someone else with this problem. My biggest difficulty is that I did not know the depth of the nested value so I wanted to find a solution that would work for deeply nested objects and without requiring to redesign anything.

    /* Retrieve the nested object value by using a string. 
       The string should be formatted by separating the properties with a period.
       @param   obj             object to pass to the function
                propertyStr     string containing properties separated by periods
       @return  nested object value. Note: may also return an object */
    
    function _nestedObjVal(obj, propertyStr) {
    var properties = propertyStr.split('.');
    if (properties.length > 1) {
        var otherProperties = propertyStr.slice(properties[0].length+1);    //separate the other properties
            return _nestedObjVal(obj[properties[0]], otherProperties);  //continue until there are no more periods in the string
    } else {
        return obj[propertyStr];
    }
    }
    
    
    function _get(name) {
        if (name.indexOf('.') !== -1) {
        //name contains nested object
            var userDefined = _nestedObjVal(plugin._optionsObj, name);
            return userDefined !== undefined ? userDefined : _nestedObjVal(plugin._defaults, name);
        } else {
            return plugin._optionsObj[name] !== undefined ?
            plugin._optionsObj[name] : plugin._defaults[name];
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Accessing nested JavaScript objects with string key Maybe the title is not
Possible Duplicate: Get list of all input objects using JavaScript, without accessing a form
Possible Duplicate: Accessing properties of an array of objects Given: [{ 'id':1, 'name':'john' },{
Possible Duplicate: JavaScript: why does parseInt(1/0, 19) return 18? Why does parseInt(1/0, 19) evaluate
Possible Duplicate: Accessing Password Protected Network Drives in Windows in C#? I have ComputerA
Possible Duplicate: Python nested functions variable scoping After much trial and error I have
Possible Duplicate: Accessing Class Properties with Spaces i have and object file and i
Possible Duplicate: Accessing the index in Python for loops I wonder: does Python have
Possible Duplicate: Accessing functions bound to event handlers with jQuery I have a around
Possible Duplicate: jQuery/JavaScript: accessing contents of an iframe If i run this on my

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.